1
Fork 0
satellite/modules/home-manager/persistence.nix

139 lines
4.9 KiB
Nix
Raw Normal View History

2024-05-21 01:37:39 +02:00
# My own module with nicer syntax for impernanence
2023-04-30 04:30:15 +02:00
{ lib, config, ... }:
let cfg = config.satellite.persistence;
in
{
2024-05-21 01:37:39 +02:00
# {{{ Option definition
2023-04-30 04:30:15 +02:00
options.satellite.persistence = {
2024-05-21 01:37:39 +02:00
enable = lib.mkEnableOption "satellite persistence";
2023-04-30 04:30:15 +02:00
at = lib.mkOption {
default = { };
description = "Record of persistent locations (eg: /persist)";
2024-05-21 01:37:39 +02:00
type = lib.types.attrsOf (lib.types.submodule (args: {
config = {
home = "${args.config.path}${config.home.homeDirectory}";
};
2023-04-30 04:30:15 +02:00
options = {
2024-05-21 01:37:39 +02:00
# {{{ Location options
2023-04-30 04:30:15 +02:00
path = lib.mkOption {
type = lib.types.str;
example = "/persist";
2024-05-21 01:37:39 +02:00
description = "The root location to store the home directory for files in this record";
};
home = lib.mkOption {
type = lib.types.str;
description = "The path to the home directory for files in this record";
2023-04-30 04:30:15 +02:00
};
2023-06-09 13:17:34 +02:00
prefixDirectories = lib.mkOption {
type = lib.types.bool;
default = true;
description = "Whether to enable gnu/stow type prefix directories";
};
2024-05-21 01:37:39 +02:00
# }}}
# {{{ Apps
2023-04-30 04:30:15 +02:00
apps = lib.mkOption {
default = { };
2024-05-21 01:37:39 +02:00
description = "Record of gnu/stow-style apps to be stored in this location";
type = lib.types.attrsOf (lib.types.submodule ({ name, ... }: {
2023-04-30 04:30:15 +02:00
options = {
2024-05-21 01:37:39 +02:00
name = lib.mkOption {
type = lib.types.str;
default = name;
description = "The gnu/stow-style subdirectory name";
};
2023-06-09 13:17:34 +02:00
files = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
example = [ ".screenrc" ];
description = ''
A list of files in your home directory you want to
link to persistent storage. Allows both absolute paths
2024-05-21 01:37:39 +02:00
and paths relative to the home directory.
2023-06-09 13:17:34 +02:00
'';
};
2023-04-30 04:30:15 +02:00
directories = lib.mkOption {
default = [ ];
2024-05-21 01:37:39 +02:00
description = ''
Modified version of `home.persistence.*.directories` which takes in absolute paths.
'';
2023-04-30 04:30:15 +02:00
type = lib.types.listOf (lib.types.either lib.types.str (lib.types.submodule {
options = {
directory = lib.mkOption {
type = lib.types.str;
2024-05-21 01:37:39 +02:00
example = "/home/username/.config/nvim";
2023-04-30 04:30:15 +02:00
description = "The directory path to be linked.";
};
method = lib.mkOption {
type = lib.types.enum [ "bindfs" "symlink" ];
2023-06-09 13:17:34 +02:00
default = "symlink";
2023-04-30 04:30:15 +02:00
description = ''
The linking method that should be used for this
directory. bindfs is the default and works for most use
cases, however some programs may behave better with
symlinks.
'';
};
};
}));
};
};
}));
};
2024-05-21 01:37:39 +02:00
# }}}
2023-04-30 04:30:15 +02:00
};
}));
};
};
2024-05-21 01:37:39 +02:00
# }}}
# {{{ Config generation
2023-04-30 04:30:15 +02:00
config =
let
makeLocation = location:
let
2024-05-21 01:37:39 +02:00
# {{{ Path processing
processPath = appName: path:
2023-06-09 13:17:34 +02:00
let
2024-05-21 01:37:39 +02:00
suffix = "${lib.strings.removePrefix "${config.home.homeDirectory}/" (builtins.toString path)}";
2023-06-09 13:17:34 +02:00
prefix = if location.prefixDirectories then "${appName}/" else "";
in
2024-05-21 01:37:39 +02:00
# lib.debug.traceSeq "\nProcessing path at location ${location.path} and app ${appName} from original path ${value} to ${prefix + suffix}"
2023-06-09 13:17:34 +02:00
(prefix + suffix);
2024-05-21 01:37:39 +02:00
# }}}
# {{{ Constructors
2023-04-30 04:30:15 +02:00
mkDirectory = appName: directory:
if builtins.isAttrs directory then {
method = directory.method;
directory = processPath appName directory.directory;
}
else processPath appName directory;
2024-05-21 01:37:39 +02:00
mkAppDirectory = app: builtins.map (mkDirectory app.name) app.directories;
mkAppFiles = app: builtins.map (processPath app.name) app.files;
# }}}
2023-04-30 04:30:15 +02:00
in
2024-05-21 01:37:39 +02:00
# {{{ Impermanence config generation
lib.attrsets.nameValuePair location.home {
2023-06-09 13:17:34 +02:00
removePrefixDirectory = location.prefixDirectories;
2023-04-30 04:30:15 +02:00
allowOther = true;
directories = lib.lists.flatten
2024-05-21 01:37:39 +02:00
(lib.attrsets.mapAttrsToList (_: mkAppDirectory) location.apps);
2023-06-09 13:17:34 +02:00
files = lib.lists.flatten
2024-05-21 01:37:39 +02:00
(lib.attrsets.mapAttrsToList (_: mkAppFiles) location.apps);
2023-04-30 04:30:15 +02:00
};
2024-05-21 01:37:39 +02:00
# }}}
2023-04-30 04:30:15 +02:00
in
lib.mkIf cfg.enable {
home.persistence = lib.attrsets.mapAttrs' (_: makeLocation) cfg.at;
};
2024-05-21 01:37:39 +02:00
# }}}
2023-04-30 04:30:15 +02:00
}