From 413b6ee6694c93be5d31ee24a3772eb3a618c8f3 Mon Sep 17 00:00:00 2001 From: Gabriel Fontes Date: Wed, 8 May 2024 14:59:18 -0300 Subject: [PATCH 1/6] 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 1d8b8c7..8bd2e40 100644 --- a/home-manager.nix +++ b/home-manager.nix @@ -6,10 +6,9 @@ let persistentStorageNames = 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 @@ -57,25 +56,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 79e2eebdd57fab58fa091ab63db09361f58cd97d Mon Sep 17 00:00:00 2001 From: Gabriel Fontes Date: Wed, 8 May 2024 15:07:44 -0300 Subject: [PATCH 2/6] refactor(home): inline isSymlink and isBindfs --- home-manager.nix | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/home-manager.nix b/home-manager.nix index 8bd2e40..03d7ed0 100644 --- a/home-manager.nix +++ b/home-manager.nix @@ -7,8 +7,6 @@ let persistentStorageNames = attrNames cfg; getDirPath = v: v.directory; - isBindfs = v: v.method == "bindfs"; - isSymlink = v: v.method == "symlink"; inherit (pkgs.callPackage ./lib.nix { }) splitPath @@ -219,7 +217,7 @@ in listToAttrs (map (mkLinkNameValuePair persistentStorageName) (map getDirPath (cfg.${persistentStorageName}.files ++ - (filter isSymlink cfg.${persistentStorageName}.directories))) + (filter (v: v.method == "symlink") cfg.${persistentStorageName}.directories))) ); in foldl' recursiveUpdate { } (map mkLinksToPersistentStorage persistentStorageNames); @@ -294,7 +292,7 @@ in mkBindMountServicesForPath = persistentStorageName: listToAttrs (map (mkBindMountService persistentStorageName) - (map getDirPath (filter isBindfs cfg.${persistentStorageName}.directories)) + (map getDirPath (filter (v: v.method == "bindfs") cfg.${persistentStorageName}.directories)) ); in builtins.foldl' @@ -358,7 +356,7 @@ in mkBindMountsForPath = persistentStorageName: concatMapStrings (mkBindMount persistentStorageName) - (map getDirPath (filter isBindfs cfg.${persistentStorageName}.directories)); + (map getDirPath (filter (v: v.method == "bindfs") cfg.${persistentStorageName}.directories)); mkUnmount = persistentStorageName: dir: let @@ -378,7 +376,7 @@ in mkUnmountsForPath = persistentStorageName: concatMapStrings (mkUnmount persistentStorageName) - (map getDirPath (filter isBindfs cfg.${persistentStorageName}.directories)); + (map getDirPath (filter (v: v.method == "bindfs") cfg.${persistentStorageName}.directories)); mkLinkCleanup = persistentStorageName: dir: let @@ -403,12 +401,12 @@ in mkLinkCleanupForPath = persistentStorageName: concatMapStrings (mkLinkCleanup persistentStorageName) - (map getDirPath (filter isSymlink cfg.${persistentStorageName}.directories)); + (map getDirPath (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 @@ -417,7 +415,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" ] @@ -447,7 +445,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" ] @@ -457,7 +455,7 @@ in (targetFilePath: '' mkdir -p ${escapeShellArg (concatPaths [ cfg.${persistentStorageName}.persistentStoragePath (dirOf targetFilePath) ])} '') - (map getDirPath (cfg.${persistentStorageName}.files ++ (filter isSymlink cfg.${persistentStorageName}.directories)))) + (map getDirPath (cfg.${persistentStorageName}.files ++ (filter (v: v.method == "symlink") cfg.${persistentStorageName}.directories)))) persistentStorageNames); }) ]; From 6f0caea39696d3fdd0bf815a781cdd97ff08b778 Mon Sep 17 00:00:00 2001 From: Gabriel Fontes Date: Wed, 8 May 2024 15:12:11 -0300 Subject: [PATCH 3/6] 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 03d7ed0..90b014d 100644 --- a/home-manager.nix +++ b/home-manager.nix @@ -41,7 +41,7 @@ in home.persistence = mkOption { default = { }; type = with types; attrsOf ( - submodule ({ name, ... }: { + submodule ({ name, config, ... }: { options = { persistentStoragePath = mkOption { @@ -53,6 +53,18 @@ in ''; }; + 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 overrided on a per entry basis. + ''; + }; + directories = mkOption { type = types.listOf ( types.coercedTo types.str (directory: { inherit directory; }) (submodule { @@ -63,12 +75,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 b625922f837709cf60a8c42f0e0c4fa6f33fb275 Mon Sep 17 00:00:00 2001 From: Gabriel Fontes Date: Wed, 8 May 2024 15:21:20 -0300 Subject: [PATCH 4/6] 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 90b014d..87d7984 100644 --- a/home-manager.nix +++ b/home-manager.nix @@ -58,8 +58,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 overrided on a per entry basis. ''; @@ -78,8 +84,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 8d14ed5f28c8fb687eecba26c14642cc335230bc Mon Sep 17 00:00:00 2001 From: Gabriel Fontes Date: Wed, 8 May 2024 16:46:26 -0300 Subject: [PATCH 5/6] fix(home): order of operations causing issue with files --- home-manager.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/home-manager.nix b/home-manager.nix index 87d7984..6587843 100644 --- a/home-manager.nix +++ b/home-manager.nix @@ -234,7 +234,7 @@ in mkLinksToPersistentStorage = persistentStorageName: listToAttrs (map (mkLinkNameValuePair persistentStorageName) - (map getDirPath (cfg.${persistentStorageName}.files ++ + (cfg.${persistentStorageName}.files ++ (map getDirPath (filter (v: v.method == "symlink") cfg.${persistentStorageName}.directories))) ); in From 65a12c0542b18fa380c18364c6091088ec09f29b Mon Sep 17 00:00:00 2001 From: Gabriel Fontes Date: Wed, 8 May 2024 16:50:46 -0300 Subject: [PATCH 6/6] fix(home): order of operations causing issue with files --- home-manager.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/home-manager.nix b/home-manager.nix index 6587843..bb21d31 100644 --- a/home-manager.nix +++ b/home-manager.nix @@ -473,7 +473,7 @@ in (targetFilePath: '' mkdir -p ${escapeShellArg (concatPaths [ cfg.${persistentStorageName}.persistentStoragePath (dirOf targetFilePath) ])} '') - (map getDirPath (cfg.${persistentStorageName}.files ++ (filter (v: v.method == "symlink") cfg.${persistentStorageName}.directories)))) + (cfg.${persistentStorageName}.files ++ (map getDirPath (filter (v: v.method == "symlink") cfg.${persistentStorageName}.directories)))) persistentStorageNames); }) ];