impermanence: cleanup some previously verbose code

This commit is contained in:
2022-12-31 09:09:51 +00:00
parent 53a0b621d8
commit c1890ce82b

View File

@@ -25,13 +25,6 @@ let
# }; # };
} }
); );
# TODO: flatten these!
home-dir-defaults = {
relativeTo = "/home/colin";
};
sys-dir-defaults = {
relativeTo = "";
};
# turn a path into a name suitable for systemd # turn a path into a name suitable for systemd
cleanName = utils.escapeSystemdPath; cleanName = utils.escapeSystemdPath;
@@ -46,15 +39,16 @@ let
joinPathAbs = comps: "/" + (builtins.concatStringsSep "/" comps); joinPathAbs = comps: "/" + (builtins.concatStringsSep "/" comps);
concatPaths = paths: joinPathAbs (builtins.concatLists (builtins.map (p: splitPath p) paths)); concatPaths = paths: joinPathAbs (builtins.concatLists (builtins.map (p: splitPath p) paths));
dirOptions = defaults: types.submodule { # options for a single mountpoint / persistence
dirEntry = types.submodule {
options = { options = {
directory = mkOption {
type = types.str;
};
encryptedClearOnBoot = mkOption { encryptedClearOnBoot = mkOption {
default = false; default = false;
type = types.bool; type = types.bool;
}; };
directory = mkOption {
type = types.str;
};
user = mkOption { user = mkOption {
type = types.nullOr types.str; type = types.nullOr types.str;
default = null; default = null;
@@ -69,25 +63,21 @@ let
}; };
}; };
}; };
mkDirsOption = defaults: mkOption { # allow "bar/baz" as shorthand for { directory = "bar/baz"; }
default = []; coercedDirEntry = types.coercedTo types.str (d: { directory = d; }) dirEntry;
type = types.listOf (types.coercedTo types.str (d: { directory = d; }) (dirOptions defaults));
# apply = map (d: if isString d then { directory = d; } else d);
};
# expand user options with more context # expand user options with more context
ingestDirOption = defaults: opt: { ingestDirEntry = relativeTo: opt: {
inherit (opt) user group mode; inherit (opt) user group mode;
directory = concatPaths [ defaults.relativeTo opt.directory ]; directory = concatPaths [ relativeTo opt.directory ];
## helpful context ## helpful context
store = builtins.addErrorContext ''while ingestDirOption on ${opt.directory} with attrs ${builtins.concatStringsSep " " (attrNames opt)}'' store = getStore opt;
(getStore opt);
}; };
ingestDirOptions = defaults: opts: builtins.map (ingestDirOption defaults) opts; ingestDirEntries = relativeTo: opts: builtins.map (ingestDirEntry relativeTo) opts;
ingested-home-dirs = ingestDirOptions home-dir-defaults cfg.home-dirs; ingested-home-dirs = ingestDirEntries "/home/colin" cfg.home-dirs;
ingested-sys-dirs = ingestDirOptions sys-dir-defaults cfg.dirs; ingested-sys-dirs = ingestDirEntries "/" cfg.dirs;
ingested-dirs = ingested-home-dirs ++ ingested-sys-dirs; ingested-dirs = ingested-home-dirs ++ ingested-sys-dirs;
in in
{ {
@@ -101,8 +91,16 @@ in
type = types.bool; type = types.bool;
description = "define / to be a tmpfs. make sure to mount some other device to /nix"; description = "define / to be a tmpfs. make sure to mount some other device to /nix";
}; };
sane.impermanence.home-dirs = mkDirsOption home-dir-defaults; sane.impermanence.home-dirs = mkOption {
sane.impermanence.dirs = mkDirsOption sys-dir-defaults; default = [];
type = types.listOf coercedDirEntry;
description = "list of directories (and optional config) to persist to disk, relative to the user's home ~";
};
sane.impermanence.dirs = mkOption {
default = [];
type = types.listOf coercedDirEntry;
description = "list of directories (and optional config) to persist to disk, relative to the fs root /";
};
}; };
imports = [ imports = [
@@ -203,6 +201,7 @@ in
dir-service = config.sane.fs."${opt.directory}".service; dir-service = config.sane.fs."${opt.directory}".service;
backing-service = config.sane.fs."${backing-path}".service; backing-service = config.sane.fs."${backing-path}".service;
# pass through the perm/mode overrides
dir-opts = { dir-opts = {
user = lib.mkIf (opt.user != null) opt.user; user = lib.mkIf (opt.user != null) opt.user;
group = lib.mkIf (opt.group != null) opt.group; group = lib.mkIf (opt.group != null) opt.group;