Skip to content

Commit

Permalink
refactor(home): get rid of 'with'
Browse files Browse the repository at this point in the history
  • Loading branch information
Misterio77 committed May 8, 2024
1 parent a33ef10 commit ab38a76
Showing 1 changed file with 36 additions and 25 deletions.
61 changes: 36 additions & 25 deletions home-manager.nix
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
{ pkgs, config, lib, ... }:

with lib;
let
inherit (lib)
any
concatMapStrings
escapeShellArg
filter
mkIf
mkOption
optional
tail
types
;

cfg = config.home.persistence;

persistentStorageNames = attrNames cfg;
persistentStorageNames = lib.attrNames cfg;

getDirPath = v: if isString v then v else v.directory;
getDirPath = v: if lib.isString v then v else v.directory;
getDirMethod = v: v.method or "bindfs";
isBindfs = v: (getDirMethod v) == "bindfs";
isSymlink = v: (getDirMethod v) == "symlink";
Expand Down Expand Up @@ -43,12 +54,12 @@ in

home.persistence = mkOption {
default = { };
type = with types; attrsOf (
submodule ({ name, ... }: {
type = types.attrsOf (
types.submodule ({ name, ... }: {
options =
{
persistentStoragePath = mkOption {
type = path;
type = types.path;
default = name;
description = ''
The path to persistent storage where the real
Expand All @@ -57,10 +68,10 @@ in
};

directories = mkOption {
type = with types; listOf (either str (submodule {
type = types.listOf (types.either types.str (types.submodule {
options = {
directory = mkOption {
type = str;
type = types.str;
default = null;
description = "The directory path to be linked.";
};
Expand Down Expand Up @@ -101,7 +112,7 @@ in
};

files = mkOption {
type = with types; listOf str;
type = types.listOf types.str;
default = [ ];
example = [
".screenrc"
Expand All @@ -113,12 +124,12 @@ in
};

allowOther = mkOption {
type = with types; nullOr bool;
type = types.nullOr types.bool;
default = null;
example = true;
apply = x:
if x == null then
warn ''
lib.warn ''
home.persistence."${name}".allowOther not set; assuming 'false'.
See https://github.com/nix-community/impermanence#home-manager for more info.
''
Expand Down Expand Up @@ -166,7 +177,7 @@ in
For detailed usage, check the <link
xlink:href="https://github.com/nix-community/impermanence">documentation</link>.
'';
example = literalExpression ''
example = lib.literalExpression ''
{
"/persistent/home/talyz" = {
directories = [
Expand Down Expand Up @@ -216,13 +227,13 @@ in
};

mkLinksToPersistentStorage = persistentStorageName:
listToAttrs (map
lib.listToAttrs (map
(mkLinkNameValuePair persistentStorageName)
(map getDirPath (cfg.${persistentStorageName}.files ++
(filter isSymlink cfg.${persistentStorageName}.directories)))
);
in
foldl' recursiveUpdate { } (map mkLinksToPersistentStorage persistentStorageNames);
lib.foldl' lib.recursiveUpdate { } (map mkLinksToPersistentStorage persistentStorageNames);

systemd.user.services =
let
Expand All @@ -236,11 +247,11 @@ in
targetDir = escapeShellArg (concatPaths [ cfg.${persistentStorageName}.persistentStoragePath dir ]);
mountPoint = escapeShellArg (concatPaths [ config.home.homeDirectory mountDir ]);
name = "bindMount-${sanitizeName targetDir}";
bindfsOptions = concatStringsSep "," (
bindfsOptions = lib.concatStringsSep "," (
optional (!cfg.${persistentStorageName}.allowOther) "no-allow-other"
++ optional (versionAtLeast pkgs.bindfs.version "1.14.9") "fsname=${targetDir}"
++ optional (lib.versionAtLeast pkgs.bindfs.version "1.14.9") "fsname=${targetDir}"
);
bindfsOptionFlag = optionalString (bindfsOptions != "") (" -o " + bindfsOptions);
bindfsOptionFlag = lib.optionalString (bindfsOptions != "") (" -o " + bindfsOptions);
bindfs = "bindfs" + bindfsOptionFlag;
startScript = pkgs.writeShellScript name ''
set -eu
Expand Down Expand Up @@ -286,19 +297,19 @@ in
Type = "forking";
ExecStart = "${startScript}";
ExecStop = "${stopScript}";
Environment = "PATH=${makeBinPath [ pkgs.coreutils pkgs.util-linux pkgs.gnugrep pkgs.bindfs ]}:/run/wrappers/bin";
Environment = "PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.util-linux pkgs.gnugrep pkgs.bindfs ]}:/run/wrappers/bin";
};
};
};

mkBindMountServicesForPath = persistentStorageName:
listToAttrs (map
lib.listToAttrs (map
(mkBindMountService persistentStorageName)
(map getDirPath (filter isBindfs cfg.${persistentStorageName}.directories))
);
in
builtins.foldl'
recursiveUpdate
lib.foldl'
lib.recursiveUpdate
{ }
(map mkBindMountServicesForPath persistentStorageNames);

Expand All @@ -325,11 +336,11 @@ in
dir;
targetDir = escapeShellArg (concatPaths [ cfg.${persistentStorageName}.persistentStoragePath dir ]);
mountPoint = escapeShellArg (concatPaths [ config.home.homeDirectory mountDir ]);
bindfsOptions = concatStringsSep "," (
bindfsOptions = lib.concatStringsSep "," (
optional (!cfg.${persistentStorageName}.allowOther) "no-allow-other"
++ optional (versionAtLeast pkgs.bindfs.version "1.14.9") "fsname=${targetDir}"
++ optional (lib.versionAtLeast pkgs.bindfs.version "1.14.9") "fsname=${targetDir}"
);
bindfsOptionFlag = optionalString (bindfsOptions != "") (" -o " + bindfsOptions);
bindfsOptionFlag = lib.optionalString (bindfsOptions != "") (" -o " + bindfsOptions);
bindfs = "${pkgs.bindfs}/bin/bindfs" + bindfsOptionFlag;
systemctl = "XDG_RUNTIME_DIR=\${XDG_RUNTIME_DIR:-/run/user/$(id -u)} ${config.systemd.user.systemctlPath}";
in
Expand Down Expand Up @@ -407,7 +418,7 @@ in


in
mkMerge [
lib.mkMerge [
(mkIf (any (path: (filter isSymlink cfg.${path}.directories) != [ ]) persistentStorageNames) {
# Clean up existing empty directories in the way of links
cleanEmptyLinkTargets =
Expand Down

0 comments on commit ab38a76

Please sign in to comment.