summaryrefslogtreecommitdiffstats
path: root/modules/environment/default.nix
blob: 5368306afc328d28b2afc20ead6e5a2f2083d665 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
{ options, config, lib, pkgs, ... }:

with lib;

let
  cfg = config.environment;

  exportVariables =
    mapAttrsToList (n: v: ''export ${n}="${v}"'') cfg.variables;

  aliasCommands =
    mapAttrsToList (n: v: ''alias ${n}=${escapeShellArg v}'')
      (filterAttrs (k: v: v != null) cfg.shellAliases);
in

{
  imports = [
    (mkRemovedOptionModule [ "environment" "loginShell" ] ''
      This option was only used to change the default command in tmux.

      This has been removed in favour of changing the default command or default shell in tmux directly.
    '')
  ];

  options = {
    environment.profiles = mkOption {
      type = types.listOf types.str;
      description = "A list of profiles used to setup the global environment.";
    };

    environment.darwinConfig = mkOption {
      type = types.nullOr (types.either types.path types.str);
      default =
        if config.nixpkgs.flake.setNixPath then
          # Don’t set this for flake‐based systems.
          null
        else if config.system.stateVersion >= 6 then
          "/etc/nix-darwin/configuration.nix"
        else
          "${config.system.primaryUserHome}/.nixpkgs/darwin-configuration.nix";
      defaultText = literalExpression ''
        if config.nixpkgs.flake.setNixPath then
          # Don’t set this for flake‐based systems.
          null
        else if config.system.stateVersion >= 6 then
          "/etc/nix-darwin/configuration.nix"
        else
          "''${config.system.primaryUserHome}/.nixpkgs/darwin-configuration.nix"
      '';
      description = ''
        The path of the darwin configuration.nix used to configure the system,
        this updates the default darwin-config entry in NIX_PATH. Since this
        changes an environment variable it will only apply to new shells.

        NOTE: Changing this requires running {command}`darwin-rebuild switch -I darwin-config=/path/to/configuration.nix`
        the first time to make darwin-rebuild aware of the custom location.
      '';
    };

    environment.variables = mkOption {
      type = types.attrsOf (types.either types.str (types.listOf types.str));
      default = {};
      example = { EDITOR = "vim"; LANG = "nl_NL.UTF-8"; };
      description = ''
        A set of environment variables used in the global environment.
        These variables will be set on shell initialisation.
        The value of each variable can be either a string or a list of
        strings.  The latter is concatenated, interspersed with colon
        characters.
      '';
      apply = mapAttrs (n: v: if isList v then concatStringsSep ":" v else v);
    };

    environment.shellAliases = mkOption {
      type = types.attrsOf types.str;
      default = {};
      example = { ll = "ls -l"; };
      description = ''
        An attribute set that maps aliases (the top level attribute names in
        this option) to command strings or directly to build outputs. The
        alises are added to all users' shells.
      '';
    };

    environment.extraInit = mkOption {
      type = types.lines;
      default = "";
      description = ''
        Shell script code called during global environment initialisation
        after all variables and profileVariables have been set.
        This code is assumed to be shell-independent, which means you should
        stick to pure sh without sh word split.
      '';
    };

    environment.shellInit = mkOption {
      default = "";
      description = ''
        Shell script code called during shell initialisation.
        This code is asumed to be shell-independent, which means you should
        stick to pure sh without sh word split.
      '';
      type = types.lines;
    };

    environment.loginShellInit = mkOption {
      default = "";
      description = ''
        Shell script code called during login shell initialisation.
        This code is asumed to be shell-independent, which means you should
        stick to pure sh without sh word split.
      '';
      type = types.lines;
    };

    environment.interactiveShellInit = mkOption {
      default = "";
      description = ''
        Shell script code called during interactive shell initialisation.
        This code is asumed to be shell-independent, which means you should
        stick to pure sh without sh word split.
      '';
      type = types.lines;
    };
  };

  config = {

    # This is horrible, sorry.
    system.requiresPrimaryUser = mkIf (
      config.nix.enable
      && !config.nixpkgs.flake.setNixPath
      && config.system.stateVersion < 6
      && options.environment.darwinConfig.highestPrio == (mkOptionDefault {}).priority
    ) [
      "environment.darwinConfig"
    ];

    environment.systemPath = mkMerge [
      [ (makeBinPath cfg.profiles) ]
      (mkOrder 1200 [ "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" ])
    ];

    # Use user, default and system profiles.
    environment.profiles = mkMerge [
      (mkOrder 800 [ "$HOME/.nix-profile" ])
      [ "/run/current-system/sw" "/nix/var/nix/profiles/default" ]
    ];

    environment.extraInit = ''
       export NIX_USER_PROFILE_DIR="/nix/var/nix/profiles/per-user/$USER"
       export NIX_PROFILES="${concatStringsSep " " (reverseList cfg.profiles)}"
    '';

    environment.variables =
      {
        XDG_CONFIG_DIRS = map (path: path + "/etc/xdg") cfg.profiles;
        XDG_DATA_DIRS = map (path: path + "/share") cfg.profiles;
        EDITOR = mkDefault "nano";
        PAGER = mkDefault "less -R";
      };

    system.build.setEnvironment = pkgs.writeText "set-environment" ''
      # Prevent this file from being sourced by child shells.
      export __NIX_DARWIN_SET_ENVIRONMENT_DONE=1

      export PATH=${config.environment.systemPath}
      ${concatStringsSep "\n" exportVariables}

      # Extra initialisation
      ${cfg.extraInit}
    '';

    system.build.setAliases = pkgs.writeText "set-aliases" ''
      ${concatStringsSep "\n" aliasCommands}
    '';
  };
}