persist: change sane.persist.all to be an attrsOf that maps path to settings

This commit is contained in:
2023-01-06 11:52:28 +00:00
parent fe816e9110
commit 841a2a3bcb
2 changed files with 50 additions and 25 deletions

View File

@@ -3,20 +3,35 @@
let let
path = sane-lib.path; path = sane-lib.path;
cfg = config.sane.persist; cfg = config.sane.persist;
mapDirs = relativeTo: store: dirs: (map
(d: { # take a directory attrset and fix its directory to be absolute
inherit (d) user group mode; fixDir = relativeTo: dir: dir // {
directory = path.concat [ relativeTo d.directory ]; directory = path.concat [ relativeTo dir.directory ];
};
fixDirs = relativeTo: dirs: map (fixDir relativeTo) dirs;
# set the `store` attribute on one dir attrset
fixStore = store: dir: dir // {
store = cfg.stores."${store}"; store = cfg.stores."${store}";
}) };
dirs # String -> [a] -> [a]
); # usually called on an attrset to map (AttrSet [a]) -> [a]
mapDirSets = relativeTo: dirsSubOptions: let fixStoreForDirs = store: dirs: map (fixStore store) dirs;
# list where each elem is a list from calling mapDirs on one store at a time
contextFreeDirSets = lib.mapAttrsToList (mapDirs relativeTo) dirsSubOptions; # populate the `store` attr for all the substores in home
in unfixed-home-dirs = builtins.concatLists (lib.mapAttrsToList fixStoreForDirs cfg.home);
builtins.concatLists contextFreeDirSets; # populate the `store` attr for all the substores in sys
unfixed-sys-dirs = builtins.concatLists (lib.mapAttrsToList fixStoreForDirs cfg.sys);
fixed-dirs = (fixDirs "/home/colin" unfixed-home-dirs) ++ (fixDirs "/" unfixed-sys-dirs);
dirToAttr = dir: {
name = dir.directory;
value = {
inherit (dir) user group mode store;
};
};
in in
{ {
sane.persist.all = (mapDirSets "/home/colin" cfg.home) ++ (mapDirSets "/" cfg.sys); sane.persist.all = builtins.listToAttrs (map dirToAttr fixed-dirs);
} }

View File

@@ -81,6 +81,15 @@ let
} }
]; ];
contextFreeDirSpec = types.submodule {
options = {
inherit (sane-types.aclOverrideMod.options) user group mode;
store = mkOption {
type = storeType;
};
};
};
# attrset from { "${storeName}" = [ dirEntry ] } # attrset from { "${storeName}" = [ dirEntry ] }
# the user can specify something like: # the user can specify something like:
@@ -110,7 +119,7 @@ in
type = dirsSubModule; type = dirsSubModule;
}; };
sane.persist.all = mkOption { sane.persist.all = mkOption {
type = types.listOf contextFreeDir; type = types.attrsOf contextFreeDirSpec;
description = "all directories known to the config. auto-computed: users should not set this directly."; description = "all directories known to the config. auto-computed: users should not set this directly.";
}; };
sane.persist.stores = mkOption { sane.persist.stores = mkOption {
@@ -129,7 +138,7 @@ in
]; ];
config = let config = let
cfgFor = opt: cfgFor = fspath: opt:
let let
store = opt.store; store = opt.store;
fsPathToStoreRelPath = fspath: path.from store.prefix fspath; fsPathToStoreRelPath = fspath: path.from store.prefix fspath;
@@ -142,27 +151,28 @@ in
in [ in [
{ {
# create destination dir, with correct perms # create destination dir, with correct perms
sane.fs."${opt.directory}" = { sane.fs."${fspath}" = {
# inherit perms & make sure we don't mount until after the mount point is setup correctly. # inherit perms & make sure we don't mount until after the mount point is setup correctly.
dir.acl = dir-acl; dir.acl = dir-acl;
mount.bind = fsPathToBackingPath opt.directory; mount.bind = fsPathToBackingPath fspath;
inherit (store.defaultOrdering) wantedBy wantedBeforeBy; inherit (store.defaultOrdering) wantedBy wantedBeforeBy;
}; };
# create the backing path as a dir # create the backing path as a dir
sane.fs."${fsPathToBackingPath opt.directory}".dir = {}; sane.fs."${fsPathToBackingPath fspath}".dir = {};
# sane.fs."${fsPathToBackingPath opt.directory}".dir.acl = config.sane.fs."${opt.directory}".generated.acl;
} }
{ {
# default each item along the backing path to have the same acl as the location it would be mounted. # default each item along the backing path to have the same acl as the location it would be mounted.
sane.fs = sane-lib.mapToAttrs (fspath: { sane.fs = sane-lib.mapToAttrs (fsSubpath: {
name = fsPathToBackingPath fspath; name = fsPathToBackingPath fsSubpath;
value.generated.acl = config.sane.fs."${fspath}".generated.acl; value.generated.acl = config.sane.fs."${fsSubpath}".generated.acl;
}) (path.walk store.prefix opt.directory); }) (path.walk store.prefix fspath);
} }
]; ];
configsPerPath = lib.mapAttrsToList cfgFor cfg.all;
allConfigs = builtins.concatLists configsPerPath;
in mkIf cfg.enable { in mkIf cfg.enable {
sane.fs = lib.mkMerge (map (c: c.sane.fs) (concatMap cfgFor cfg.all)); sane.fs = lib.mkMerge (map (c: c.sane.fs) allConfigs);
}; };
} }