summaryrefslogtreecommitdiff
path: root/modules/devenv/dotfiles.nix
blob: 8b8d95b170eb0381c34631ffb45307044748f3e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
{
  config,
  lib,
  ...
}: let
  cfg = config.dotfiles;
  dotfilesToIgnore = lib.attrNames (lib.filterAttrs (_: {gitignore, ...}: gitignore) cfg);
in {
  options.dotfiles = lib.mkOption {
    type = with lib.types;
    # this cannot be a lazyAttrsOf, see https://nixos.org/manual/nixos/unstable/#sec-option-types-composed
      attrsOf (submodule {
        options = {
          gitignore =
            lib.mkEnableOption ""
            // {
              default = true;
              description = "Whether git should ignore this dotfile, typically if it is generated to contain absolute paths.";
            };
          text = lib.mkOption {
            type = lib.types.lines;
            default = "";
            description = "The content of the dotfile.";
          };
        };
      });
  };

  config.enterShell =
    lib.mkIf (cfg != {})
    (''
        echo Installing dotfiles...
      ''
      + lib.concatStringsSep "\n" (lib.mapAttrsToList (
          name: {
            text,
            gitignore,
          }:
          # this has to be done here to avoid infinite recursion
          let
            content =
              text
              + lib.optionalString (name == ".gitignore") ''
                # dotfiles
                ${lib.concatStringsSep "\n" dotfilesToIgnore}
              '';
          in ''
            ${
              if gitignore
              then "ln -s"
              else "cp"
            } "${builtins.toFile name content}" "${name}"
          ''
        )
        cfg));
}