From ecbc751c598c6a7048e53c37bc4b31e448314e3e Mon Sep 17 00:00:00 2001 From: Gabriel Fontes Date: Wed, 8 May 2024 14:59:18 -0300 Subject: [PATCH 1/5] refactor(home): use coercedTo with directory type --- home-manager.nix | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/home-manager.nix b/home-manager.nix index 92cfcdb..ee7b73c 100644 --- a/home-manager.nix +++ b/home-manager.nix @@ -6,10 +6,9 @@ let persistentStorageNames = (filter (path: cfg.${path}.enable) (attrNames cfg)); - getDirPath = v: if isString v then v else v.directory; - getDirMethod = v: v.method or "bindfs"; - isBindfs = v: (getDirMethod v) == "bindfs"; - isSymlink = v: (getDirMethod v) == "symlink"; + getDirPath = v: v.directory; + isBindfs = v: v.method == "bindfs"; + isSymlink = v: v.method == "symlink"; inherit (pkgs.callPackage ./lib.nix { }) splitPath @@ -63,25 +62,26 @@ in }; directories = mkOption { - type = with types; listOf (either str (submodule { - options = { - directory = mkOption { - type = str; - default = null; - description = "The directory path to be linked."; + type = types.listOf ( + types.coercedTo types.str (directory: { inherit directory; }) (submodule { + options = { + directory = mkOption { + type = str; + description = "The directory path to be linked."; + }; + method = mkOption { + type = types.enum [ "bindfs" "symlink" ]; + default = "bindfs"; + 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. + ''; + }; }; - method = mkOption { - type = types.enum [ "bindfs" "symlink" ]; - default = "bindfs"; - 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. - ''; - }; - }; - })); + }) + ); default = [ ]; example = [ "Downloads" From 724729ade2a9b696cbb5828f9d56006e067765af Mon Sep 17 00:00:00 2001 From: Gabriel Fontes Date: Wed, 8 May 2024 15:07:44 -0300 Subject: [PATCH 2/5] refactor(home): inline isSymlink, isBindfs, and getDirPath --- home-manager.nix | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/home-manager.nix b/home-manager.nix index ee7b73c..055edaa 100644 --- a/home-manager.nix +++ b/home-manager.nix @@ -6,10 +6,6 @@ let persistentStorageNames = (filter (path: cfg.${path}.enable) (attrNames cfg)); - getDirPath = v: v.directory; - isBindfs = v: v.method == "bindfs"; - isSymlink = v: v.method == "symlink"; - inherit (pkgs.callPackage ./lib.nix { }) splitPath dirListToPath @@ -224,8 +220,8 @@ in mkLinksToPersistentStorage = persistentStorageName: listToAttrs (map (mkLinkNameValuePair persistentStorageName) - (map getDirPath (cfg.${persistentStorageName}.files ++ - (filter isSymlink cfg.${persistentStorageName}.directories))) + (map (v: v.directory) (cfg.${persistentStorageName}.files ++ + (filter (v: v.method == "symlink") cfg.${persistentStorageName}.directories))) ); in foldl' recursiveUpdate { } (map mkLinksToPersistentStorage persistentStorageNames); @@ -300,7 +296,7 @@ in mkBindMountServicesForPath = persistentStorageName: listToAttrs (map (mkBindMountService persistentStorageName) - (map getDirPath (filter isBindfs cfg.${persistentStorageName}.directories)) + (map (v: v.directory) (filter (v: v.method == "bindfs") cfg.${persistentStorageName}.directories)) ); in builtins.foldl' @@ -364,7 +360,7 @@ in mkBindMountsForPath = persistentStorageName: concatMapStrings (mkBindMount persistentStorageName) - (map getDirPath (filter isBindfs cfg.${persistentStorageName}.directories)); + (map (v: v.directory) (filter (v: v.method == "bindfs") cfg.${persistentStorageName}.directories)); mkUnmount = persistentStorageName: dir: let @@ -384,7 +380,7 @@ in mkUnmountsForPath = persistentStorageName: concatMapStrings (mkUnmount persistentStorageName) - (map getDirPath (filter isBindfs cfg.${persistentStorageName}.directories)); + (map (v: v.directory) (filter (v: v.method == "bindfs") cfg.${persistentStorageName}.directories)); mkLinkCleanup = persistentStorageName: dir: let @@ -409,12 +405,12 @@ in mkLinkCleanupForPath = persistentStorageName: concatMapStrings (mkLinkCleanup persistentStorageName) - (map getDirPath (filter isSymlink cfg.${persistentStorageName}.directories)); + (map (v: v.directory) (filter (v: v.method == "symlink") cfg.${persistentStorageName}.directories)); in mkMerge [ - (mkIf (any (path: (filter isSymlink cfg.${path}.directories) != [ ]) persistentStorageNames) { + (mkIf (any (path: (filter (v: v.method == "symlink") cfg.${path}.directories) != [ ]) persistentStorageNames) { # Clean up existing empty directories in the way of links cleanEmptyLinkTargets = dag.entryBefore @@ -423,7 +419,7 @@ in ${concatMapStrings mkLinkCleanupForPath persistentStorageNames} ''; }) - (mkIf (any (path: (filter isBindfs cfg.${path}.directories) != [ ]) persistentStorageNames) { + (mkIf (any (path: (filter (v: v.method == "bindfs") cfg.${path}.directories) != [ ]) persistentStorageNames) { createAndMountPersistentStoragePaths = dag.entryBefore [ "writeBoundary" ] @@ -453,7 +449,7 @@ in unmountBindMounts ''; }) - (mkIf (any (path: (cfg.${path}.files != [ ]) || ((filter isSymlink cfg.${path}.directories) != [ ])) persistentStorageNames) { + (mkIf (any (path: (cfg.${path}.files != [ ]) || ((filter (v: v.method == "symlink") cfg.${path}.directories) != [ ])) persistentStorageNames) { createTargetFileDirectories = dag.entryBefore [ "writeBoundary" ] @@ -463,7 +459,7 @@ in (targetFilePath: '' mkdir -p ${escapeShellArg (concatPaths [ cfg.${persistentStorageName}.persistentStoragePath (dirOf targetFilePath) ])} '') - (map getDirPath (cfg.${persistentStorageName}.files ++ (filter isSymlink cfg.${persistentStorageName}.directories)))) + (map (v: v.directory) (cfg.${persistentStorageName}.files ++ (filter (v: v.method == "symlink") cfg.${persistentStorageName}.directories)))) persistentStorageNames); }) ]; From 27dc868e4df454ea844920985b6f8cdcbee5f799 Mon Sep 17 00:00:00 2001 From: Gabriel Fontes Date: Wed, 8 May 2024 15:12:11 -0300 Subject: [PATCH 3/5] feat(home): add defaultDirectoryMethod --- home-manager.nix | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/home-manager.nix b/home-manager.nix index 055edaa..049d1ea 100644 --- a/home-manager.nix +++ b/home-manager.nix @@ -39,7 +39,7 @@ in home.persistence = mkOption { default = { }; type = with types; attrsOf ( - submodule ({ name, ... }: { + submodule ({ name, config, ... }: { options = { persistentStoragePath = mkOption { @@ -57,6 +57,18 @@ in description = "Whether to enable this persistent storage location."; }; + defaultDirectoryMethod = mkOption { + type = types.enum [ "bindfs" "symlink" ]; + default = "bindfs"; + description = '' + The linking method that should be used for directories. + bindfs is the default and works for most use cases, however + some programs may behave better with symlinks. + + This can be overridden on a per entry basis. + ''; + }; + directories = mkOption { type = types.listOf ( types.coercedTo types.str (directory: { inherit directory; }) (submodule { @@ -67,12 +79,11 @@ in }; method = mkOption { type = types.enum [ "bindfs" "symlink" ]; - default = "bindfs"; + default = config.defaultDirectoryMethod; 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. + The linking method to be used for this specific + directory entry. Defaults to + defaultDirectoryMethod. ''; }; }; From 69f00cf2f23b4775ad4a5e74e8504c2fe51d4480 Mon Sep 17 00:00:00 2001 From: Gabriel Fontes Date: Wed, 8 May 2024 15:21:20 -0300 Subject: [PATCH 4/5] docs(home): improve method docs --- home-manager.nix | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/home-manager.nix b/home-manager.nix index 049d1ea..53eb3c0 100644 --- a/home-manager.nix +++ b/home-manager.nix @@ -62,8 +62,14 @@ in default = "bindfs"; description = '' The linking method that should be used for directories. - bindfs is the default and works for most use cases, however - some programs may behave better with symlinks. + + - bindfs is very transparent, and thus used as a safe + default. It has, however, a significant performance impact in + IO-heavy situations. + + - symlinks have great performance but may be treated + specially by some programs that may e.g. generate + errors/warnings, or replace them. This can be overridden on a per entry basis. ''; @@ -82,8 +88,9 @@ in default = config.defaultDirectoryMethod; description = '' The linking method to be used for this specific - directory entry. Defaults to - defaultDirectoryMethod. + directory entry. See + defaultDirectoryMethod for more + information on the tradeoffs. ''; }; }; From 8825ba211a9b079f9b0c45d74645cf81b2c82b4a Mon Sep 17 00:00:00 2001 From: Gabriel Fontes Date: Wed, 8 May 2024 16:46:26 -0300 Subject: [PATCH 5/5] fix(home): order of operations causing issue with files --- home-manager.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/home-manager.nix b/home-manager.nix index 53eb3c0..ab44147 100644 --- a/home-manager.nix +++ b/home-manager.nix @@ -238,7 +238,7 @@ in mkLinksToPersistentStorage = persistentStorageName: listToAttrs (map (mkLinkNameValuePair persistentStorageName) - (map (v: v.directory) (cfg.${persistentStorageName}.files ++ + (cfg.${persistentStorageName}.files ++ (map (v: v.directory) (filter (v: v.method == "symlink") cfg.${persistentStorageName}.directories))) ); in @@ -477,7 +477,7 @@ in (targetFilePath: '' mkdir -p ${escapeShellArg (concatPaths [ cfg.${persistentStorageName}.persistentStoragePath (dirOf targetFilePath) ])} '') - (map (v: v.directory) (cfg.${persistentStorageName}.files ++ (filter (v: v.method == "symlink") cfg.${persistentStorageName}.directories)))) + (cfg.${persistentStorageName}.files ++ (map (v: v.directory) (filter (v: v.method == "symlink") cfg.${persistentStorageName}.directories)))) persistentStorageNames); }) ];