nixfiles/lib.nix

125 lines
4.7 KiB
Nix
Raw Normal View History

2022-02-06 00:06:26 +00:00
{ lib }:
2022-02-13 13:10:21 +00:00
let
2022-02-13 23:06:31 +00:00
inherit (builtins) replaceStrings elemAt mapAttrs;
2022-02-19 22:55:53 +00:00
inherit (lib)
genAttrs mapAttrs' mapAttrsToList filterAttrsRecursive nameValuePair types
mkOption mkOverride mkForce mergeEqualOption;
2022-02-13 13:10:21 +00:00
inherit (lib.flake) defaultSystems;
in
rec {
# Yoinked from nixpkgs/nixos/modules/services/networking/nat.nix
isIPv6 = ip: builtins.length (lib.splitString ":" ip) > 2;
parseIPPort = ipp:
let
v6 = isIPv6 ipp;
matchIP = if v6 then "[[]([0-9a-fA-F:]+)[]]" else "([0-9.]+)";
m = builtins.match "${matchIP}:([0-9-]+)" ipp;
checked = v: if m == null then throw "bad ip:ports `${ipp}'" else v;
in
{
inherit v6;
ip = checked (elemAt m 0);
ports = checked (replaceStrings ["-"] [":"] (elemAt m 1));
};
2022-02-19 22:55:53 +00:00
attrsToNVList = mapAttrsToList nameValuePair;
2022-02-13 23:06:31 +00:00
mkDefaultSystemsPkgs = path: args': genAttrs defaultSystems (system: import path ((args' system) // { inherit system; }));
2022-02-13 13:44:37 +00:00
mkApp = program: { type = "app"; inherit program; };
2022-02-13 14:28:49 +00:00
mkShellApp = pkgs: name: text: mkApp (pkgs.writeShellScript name text).outPath;
2022-02-14 00:56:43 +00:00
mkShellApp' = pkgs: args:
let
app = pkgs.writeShellApplication args;
in mkApp "${app}/bin/${app.meta.mainProgram}";
2022-02-13 23:06:31 +00:00
flakePackageOverlay' = flake: pkg: system: (final: prev:
let
pkg' = if pkg != null then flake.packages.${system}.${pkg} else flake.defaultPackage.${system};
name = if pkg != null then pkg else pkg'.name;
in
{
${name} = pkg';
});
flakePackageOverlay = flake: flakePackageOverlay' flake null;
2022-02-06 00:06:26 +00:00
inlineModule' = path: module: {
_file = path;
imports = [ module ];
};
inlineModule = path: inlineModule' path (import path);
2022-02-19 22:55:53 +00:00
# Merge together modules which are defined as functions with others that aren't
naiveModule = with types; (coercedTo (attrsOf anything) (conf: { ... }: conf) (functionTo (attrsOf anything)));
2022-02-13 13:10:21 +00:00
mkOpt = type: default: mkOption { inherit type default; };
2022-02-13 17:44:14 +00:00
mkOpt' = type: default: description: mkOption { inherit type default description; };
2022-02-13 13:10:21 +00:00
mkBoolOpt = default: mkOption {
inherit default;
type = types.bool;
example = true;
};
2022-02-13 17:44:14 +00:00
mkBoolOpt' = default: description: mkOption {
inherit default description;
type = types.bool;
example = true;
};
2022-02-19 22:55:53 +00:00
nullOrOpt' = type: description: mkOpt' (types.nullOr type) null description;
2022-02-13 13:10:21 +00:00
dummyOption = mkOption { };
2022-02-13 17:44:14 +00:00
2022-02-19 22:55:53 +00:00
# Slightly higher precedence than mkDefault
mkDefault' = mkOverride 900;
2022-02-13 17:44:14 +00:00
mkVMOverride' = mkOverride 9;
homeStateVersion = hmBranch: {
# The flake passes a default setting, but we don't care about that
home.stateVersion = mkForce (if hmBranch == "unstable" then "22.05" else "21.11");
};
2022-02-19 22:55:53 +00:00
commonOpts = with types; {
moduleType = mkOptionType {
name = "Inline flake-exportable module";
merge = loc: defs: inlineModule (mergeEqualOption loc defs);
};
system = mkOpt' (enum defaultSystems) null "Nix-style system string.";
nixpkgs = mkOpt' (enum [ "master" "unstable" "stable" "mine" ]) "unstable" "Branch of nixpkgs to use.";
home-manager = mkOpt' (enum [ "unstable" "stable" ]) "unstable" "Branch of home-manager to use.";
};
2022-02-19 22:55:53 +00:00
deploy-rs =
with types;
let
globalOpts = {
sshUser = nullOrOpt' str "Username deploy-rs will deploy with.";
user = nullOrOpt' str "Username deploy-rs will deploy with.";
sudo = nullOrOpt' str "Command to elevate privileges with (used if the deployment user != profile user).";
sshOpts = nullOrOpt' (listOf str)
"Options deploy-rs will pass to ssh. Note: overriding at a lower level _merges_ options.";
fastConnection = nullOrOpt' bool "Whether to copy the whole closure instead of using substitution.";
autoRollback = nullOrOpt' bool "Whether to roll back the profile if activation fails.";
magicRollback = nullOrOpt' bool "Whether to roll back the profile if connectivity to the deployer is lost.";
confirmTimeout = nullOrOpt' ints.u16 "Timeout for confirming activation succeeded.";
tempPath = nullOrOpt' str "Path that deploy-rs will use for temporary files.";
};
profileOpts = {
path = mkOpt' package "" "Derivation to build (should include activation script).";
profilePath = nullOrOpt' str "Path to profile location";
} // globalOpts;
profile = submodule { options = profileOpts; };
nodeOpts = {
hostname = mkOpt' str "" "Hostname deploy-rs will connect to.";
profilesOrder = nullOrOpt' (listOf str)
"Order to deploy profiles in (remainder will be deployed in arbitrary order).";
profiles = mkOpt' (attrsOf profile) { } "Profiles to deploy.";
} // globalOpts;
in
rec {
inherit profile;
node = submodule { options = nodeOpts; };
filterOpts = filterAttrsRecursive (_: v: v != null);
};
authorizedKeys = toString ./authorized_keys;
2022-02-13 13:10:21 +00:00
}