summaryrefslogtreecommitdiff
path: root/modules/common/nix.nix
blob: aad710651eb73397bc92498736c1a051ff965ef8 (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
{
  config,
  inputs,
  lib,
  pkgs,
  this,
  ...
}:
with lib;
let
  cfg = config.nixfiles.modules.common.nix;
in
{
  imports = with inputs.srvos.nixosModules; [
    mixins-nix-experimental
    mixins-trusted-nix-caches
  ];

  options.nixfiles.modules.common.nix.allowedUnfreePackages = mkOption {
    description = "A list of allowed unfree packages.";
    type = with types; listOf str;
    default = [ ];
  };

  config =
    let
      useNixpkgs =
        nixpkgs:
        import nixpkgs {
          inherit (config.nixpkgs) config;
          inherit (this) system;
        };

      pkgsLocal = useNixpkgs "${config.my.home}/src/nixpkgs"; # Impure!
      pkgsMaster = useNixpkgs inputs.nixpkgs-master;
      pkgsStable = useNixpkgs inputs.nixpkgs-stable;
      pkgsRev =
        rev: hash:
        useNixpkgs (
          pkgs.fetchFromGitHub {
            owner = "NixOS";
            repo = "nixpkgs";
            inherit rev hash;
          }
        );
      pkgsPr = pr: pkgsRev "refs/pull/${toString pr}/head";
    in
    {
      _module.args = {
        inherit
          pkgsLocal
          pkgsMaster
          pkgsStable
          pkgsRev
          pkgsPr
          ;
      };

      hm = {
        # Used primarily in conjunction with the "nixfiles" script.
        home.file.".nix-defexpr/default.nix".text =
          let
            hostname = strings.escapeNixIdentifier this.hostname;
          in
          optionalString this.isHeadful ''
            let
              self = builtins.getFlake "nixfiles";
              configurations = self.nixosConfigurations;
              local = configurations.${hostname};
            in rec {
              inherit self;
              inherit (self) inputs lib;
              inherit (lib) my;
              this = my.configurations.${hostname};
              inherit (local) config;
              inherit (local.config.system.build) toplevel vm vmWithBootLoader manual;
              pretty = expr: lib.trace (lib.generators.toPretty {} expr) {};
            } // configurations // local._module.args
          '';

        programs.bash.shellAliases.nix = "nix --verbose --print-build-logs";
      };

      nix =
        let
          notSelfInputs = filterAttrs (n: _: n != "self") inputs;
        in
        {
          daemonCPUSchedPolicy = "idle";
          daemonIOSchedClass = "idle";
          daemonIOSchedPriority = 7;

          settings = {
            keep-derivations = if this.isHeadful then "true" else "false";
            keep-outputs = if this.isHeadful then "true" else "false";

            warn-dirty = false;

            keep-going = true;

            substituters = [
              "https://azahi.cachix.org"
              "https://nix-community.cachix.org"
            ];

            trusted-substituters = [ "https://azahi.cachix.org" ];
            trusted-public-keys = [ "azahi.cachix.org-1:2bayb+iWYMAVw3ZdEpVg+NPOHCXncw7WMQ0ElX1GO3s=" ];

            trusted-users = [
              "root"
              my.username
            ];
          };

          nixPath = mapAttrsToList (n: v: "${n}=${v}") notSelfInputs ++ [
            "nixfiles=${config.my.home}/src/nixfiles"
          ];

          registry = mapAttrs (_: flake: { inherit flake; }) notSelfInputs // {
            nixfiles.flake = inputs.self;
          };
        };

      nixpkgs = {
        config.allowUnfreePredicate = p: elem (getName p) cfg.allowedUnfreePackages;

        overlays = with inputs; [
          self.overlays.default
          (_: prev: {
            # Global PR package overrides go here. Example:
            # ```
            # inherit (pkgsPr 309018 "sha256-x3ATxjrTVdaX5eo9P6pz+8/W6D2TNYzvjZpOBa3ZRI8=") endlessh-go;
            # ```

            spf-engine = prev.spf-engine.override {
              # FIXME https://nixpk.gs/pr-tracker.html?pr=321940
              inherit (pkgsMaster.python311.pkgs) pymilter;
            };
          })
        ];
      };

      environment = {
        defaultPackages = [ ];
        systemPackages =
          with pkgs;
          optionals this.isHeadful [
            nix-tree
            nixfiles
          ];
        variables = {
          NIXFILES = optionalString this.isHeadful "${config.my.home}/src/nixfiles";
          NIX_SHELL_PRESERVE_PROMPT = "1";
        };
      };

      system.stateVersion = this.stateVersion or trivial.release;
    };
}