impermanence/home-manager.nix

96 lines
2.5 KiB
Nix
Raw Normal View History

2020-06-04 21:46:38 +00:00
{ pkgs, config, lib, ... }:
with lib;
let
cfg = config.home.persistence;
persistentStoragePaths = attrNames cfg;
inherit (pkgs.callPackage ./lib.nix { }) splitPath dirListToPath concatPaths;
in
{
options = {
home.persistence = mkOption {
default = { };
2020-06-05 17:37:07 +00:00
type = with types; attrsOf (
submodule {
2020-06-04 21:46:38 +00:00
options =
{
directories = mkOption {
type = with types; listOf string;
default = [ ];
};
files = mkOption {
type = with types; listOf string;
default = [ ];
};
removePrefixDirectory = mkOption {
type = types.bool;
default = false;
};
};
}
);
};
};
config = {
home.file =
let
link = file:
pkgs.runCommand
"${replaceStrings [ "/" "." " " ] [ "-" "" "" ] file}"
{ }
"ln -s '${file}' $out";
mkLinkNameValuePair = persistentStoragePath: fileOrDir: {
2020-06-05 17:37:07 +00:00
name =
if cfg.${persistentStoragePath}.removePrefixDirectory then
dirListToPath (tail (splitPath [ fileOrDir ]))
else
fileOrDir;
2020-06-04 21:46:38 +00:00
value = { source = link (concatPaths [ persistentStoragePath fileOrDir ]); };
};
mkLinksToPersistentStorage = persistentStoragePath:
2020-06-05 17:37:07 +00:00
listToAttrs (map
(mkLinkNameValuePair persistentStoragePath)
(cfg.${persistentStoragePath}.files ++ cfg.${persistentStoragePath}.directories)
);
2020-06-04 21:46:38 +00:00
in
2020-06-05 17:37:07 +00:00
foldl' recursiveUpdate { } (map mkLinksToPersistentStorage persistentStoragePaths);
2020-06-04 21:46:38 +00:00
home.activation =
let
dag = config.lib.dag;
mkDirCreationSnippet = persistentStoragePath: dir:
let
targetDir = concatPaths [ persistentStoragePath dir ];
2020-06-05 17:37:07 +00:00
in
''
2020-06-04 21:46:38 +00:00
if [[ ! -e "${targetDir}" ]]; then
mkdir -p "${targetDir}"
fi
'';
mkDirCreationScriptForPath = persistentStoragePath: {
name = "createDirsIn-${replaceStrings [ "/" "." " " ] [ "-" "" "" ] persistentStoragePath}";
2020-06-04 21:46:38 +00:00
value =
dag.entryAfter
[ "writeBoundary" ]
(concatMapStrings
(mkDirCreationSnippet persistentStoragePath)
2020-06-05 17:37:07 +00:00
cfg.${persistentStoragePath}.directories
);
2020-06-04 21:46:38 +00:00
};
in
2020-06-05 17:37:07 +00:00
listToAttrs (map mkDirCreationScriptForPath persistentStoragePaths);
2020-06-04 21:46:38 +00:00
};
}