dot

NixOS dotfiles
git clone https://git.echoz.io/dot.git
Log | Files | Refs

default.nix (2503B)


      1 {
      2   home-manager.sharedModules = [
      3     (
      4       { lib, config, ... }:
      5       let
      6         fileOptionAttrPaths = [
      7           [
      8             "home"
      9             "file"
     10           ]
     11           [
     12             "xdg"
     13             "configFile"
     14           ]
     15           [
     16             "xdg"
     17             "dataFile"
     18           ]
     19         ];
     20 
     21         mergeAttrsList = builtins.foldl' (lib.mergeAttrs) { };
     22 
     23         fileAttrsType = lib.types.attrsOf (
     24           lib.types.submodule ({
     25             options.mutable = lib.mkOption {
     26               type = lib.types.bool;
     27               default = false;
     28               description = ''
     29                 Whether to copy the file without the read-only attribute instead of
     30                 symlinking. If you set this to `true`, you must also set `force` to
     31                 `true`. Mutable files are not removed when you remove them from your
     32                 configuration.
     33 
     34                 This option is useful for programs that don't have a very good
     35                 support for read-only configurations.
     36               '';
     37             };
     38           })
     39         );
     40       in
     41       {
     42         options = mergeAttrsList (
     43           map (
     44             attrPath: lib.setAttrByPath attrPath (lib.mkOption { type = fileAttrsType; })
     45           ) fileOptionAttrPaths
     46         );
     47 
     48         config.home.activation.mutableFileGeneration =
     49           let
     50             allFiles = (
     51               builtins.concatLists (
     52                 map (attrPath: builtins.attrValues (lib.getAttrFromPath attrPath config)) fileOptionAttrPaths
     53               )
     54             );
     55 
     56             filterMutableFiles = builtins.filter (
     57               file:
     58               (file.mutable or false)
     59               && lib.assertMsg file.force "if you set `mutable` to `true` on a file you must also set `force` to `true`"
     60             );
     61 
     62             mutableFiles = filterMutableFiles allFiles;
     63 
     64             toCommand = (
     65               file:
     66               let
     67                 source = lib.escapeShellArg file.source;
     68                 target = lib.escapeShellArg file.target;
     69               in
     70               ''
     71                 $VERBOSE_ECHO "${source} -> ${target}"
     72                 $DRY_RUN_CMD cp --remove-destination --no-preserve=mode ${source} ${target}
     73               ''
     74             );
     75 
     76             command = ''
     77               echo "Copying mutable home files for $HOME"
     78             ''
     79             + lib.concatLines (map toCommand mutableFiles);
     80           in
     81           (lib.hm.dag.entryAfter [ "linkGeneration" ] command);
     82       }
     83     )
     84   ];
     85 }