diff options
| author | Mel <einebeere@gmail.com> | 2025-02-09 18:24:45 +0100 |
|---|---|---|
| committer | Mel <einebeere@gmail.com> | 2025-02-09 18:24:45 +0100 |
| commit | e5cac4cde47a4b351bd0919b25a391ad7184616f (patch) | |
| tree | 2ebb86ab328f47d39ffde4425b4263717221de62 /modules/foundation/home | |
| parent | 11f5bfd68d0fa7a3e65166502104badf07367134 (diff) | |
| download | minerals-e5cac4cde47a4b351bd0919b25a391ad7184616f.tar.zst minerals-e5cac4cde47a4b351bd0919b25a391ad7184616f.zip | |
Allow home-manager to create mutable config files for programs like VSCode
Signed-off-by: Mel <einebeere@gmail.com>
Diffstat (limited to 'modules/foundation/home')
| -rw-r--r-- | modules/foundation/home/default.nix | 7 | ||||
| -rw-r--r-- | modules/foundation/home/mutable.nix | 81 |
2 files changed, 88 insertions, 0 deletions
diff --git a/modules/foundation/home/default.nix b/modules/foundation/home/default.nix new file mode 100644 index 0000000..627dac6 --- /dev/null +++ b/modules/foundation/home/default.nix @@ -0,0 +1,7 @@ +{ ... }: + +{ + imports = [ + ./mutable.nix + ]; +} diff --git a/modules/foundation/home/mutable.nix b/modules/foundation/home/mutable.nix new file mode 100644 index 0000000..76f44af --- /dev/null +++ b/modules/foundation/home/mutable.nix @@ -0,0 +1,81 @@ +# this module is heavily inspired by a home-manager module +# created by @piousdeer, although improved and slightly simplified. +# see: https://gist.github.com/piousdeer/b29c272eaeba398b864da6abf6cb5daa +{ config, lib, ... }: + +let + inherit (lib) + hm + types + assertMsg + mkOption + escapeShellArg + splitString + recursiveUpdate + setAttrByPath + concatLines + ; + + optionPathsToExtend = [ + "home.file" + "xdg.configFile" + "xdg.dataFile" + "xdg.stateFile" + ]; +in +{ + options = + let + # like `mergeAttrsList`, but doesn't overwrite deep paths, + # adding to the inner attrsets instead. + recursiveMergeAttrsList = builtins.foldl' recursiveUpdate { }; + + path = p: splitString "." p; + + mutableOption = mkOption { + type = types.attrsOf ( + types.submodule { + options.mutable = mkOption { + type = types.bool; + default = false; + description = '' + Instead of linking from this file to the nix store, + copy the contents to the path directly, thus making + the file writable to programs. + Requires the `force` option to be true. + ''; + }; + } + ); + }; + in + recursiveMergeAttrsList (map (p: setAttrByPath (path p) mutableOption) optionPathsToExtend); + + config = { + home.activation.overwriteMutableFiles = hm.dag.entryAfter [ "linkGeneration" ] ( + let + # all xdg files end up inside the normal `home.file` + # list either way, we don't have to look into the others. + cfg = config.home.file // config.xdg.configFile; + allFiles = builtins.attrValues cfg; + mutableFiles = builtins.filter ( + f: (f.mutable or false) && assertMsg f.force "`mutable` also requires `force` to be set to true" + ) allFiles; + + copyFileCommand = + { source, target, ... }: + '' + run cp $VERBOSE_ARG --remove-destination --no-preserve=mode \ + ${escapeShellArg source} ${escapeShellArg target} \ + || errorEcho "Overwriting mutable file '${escapeShellArg target}' failed!" + ''; + in + '' + ${config.lib.bash.initHomeManagerLib} + + verboseEcho "Overwriting symlinks with mutable files" + ${concatLines (map copyFileCommand mutableFiles)} + '' + ); + }; +} |
