summaryrefslogtreecommitdiff
path: root/modules/devenv/dotfiles.nix
blob: 994b50f05b44f4ff354dc4ea4e37b8024cfd79a6 (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
57
58
59
60
61
{
  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 "" // {
            description = ''
              Whether git should ignore this dotfile, typically if it is
              generated to contain absolute paths and is specific to this
              project (and may not be ignored system-wide by the user).
            '';
          };
          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" && dotfilesToIgnore != [ ]) ''
              ### dotfiles
              ${lib.concatStringsSep "\n" dotfilesToIgnore}
            '';
        in
        ''
          ${
            if gitignore then "ln --symbolic --force" else "install --mode=644"
          } "${builtins.toFile name content}" "${name}"
        ''
      ) cfg
    )
  );
}