Summary Files Commits Branches @commit @diff @stats
path: root / autoconfig.nix
blob: f092bd75cb153aa388777fa0c48ea53c4b96e03b (plain) (blame)
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
{ inputs, modules ? [], ... }:
let
  inherit (inputs) nixpkgs;
  inherit (nixpkgs) lib;

  custom-packages = system:
    let
      pkgs' = import nixpkgs.legacyPackages.${system}.path { inherit system; config.allowUnfree = true; };
      packages = lib.mapAttrs
        (name: _:
          pkgs'.callPackage ./packages/${name} (packages // { inherit inputs; }))
        (lib.filterAttrs (_: type: type == "directory") (builtins.readDir ./packages));
    in
      packages;

  other-nixpkgs = system:
    lib.mapAttrs
    (_: nixpkgs-special:
      nixpkgs-special.legacyPackages.${system} or nixpkgs-special.packages.${system})
    (lib.filterAttrs
      (name: _:
        name != "nixpkgs")
      inputs);

  package-overlays =
    lib.mapAttrsToList
    (name: _: (import ./overlays/${name}))
    (builtins.readDir ./overlays);

in {
  configurations =
    lib.mapAttrs
    (dir: _:
      let
        # sysidentity.nix is a file inside machines/name/, which is always in the form
        # groups: # attrset which will be used in include and exclude
        # {
        #   name = ""; # hostname of machine
        #   arch = ""; # architecture
        #   kind = ""; # name of subfolder inside ./modules
        #   include = []; # names of files inside ./modules/kind/ which to be included, by default only default.nix is
        #   exclude = []; # names of files inside ./modules/kind/ which to be excluded (from include)
        # }
        sysidentity =
          let
            group-definitions = import ./module-groups.nix;
            func-sysidentity = import ./machines/${dir}/sysidentity.nix;
            inherit (func-sysidentity { }) kind;
          in
            func-sysidentity (group-definitions.${kind} // group-definitions.all or {});

        modules-dir = lib.path.append ./modules sysidentity."kind";

        package-modules =
          let
            getmodules = attrname:
              if sysidentity ? ${attrname}
              then map
                    (v: lib.path.append modules-dir (v + (if lib.hasSuffix ".nix" v then "" else ".nix")))
                    sysidentity.${attrname}
              else [];

            includelist = getmodules "include";
            excludelist = getmodules "exclude";
          in
            lib.subtractLists excludelist includelist;

        custom-packages-overlays = custom-packages sysidentity.arch;

      in
      lib.nixosSystem {
        system = sysidentity.arch;
        modules = [
          ./machines/${dir}/hardware-configuration.nix
          ./configuration.nix
          modules-dir
          ({ ... }: {
            networking.hostName = sysidentity.name;
            nixpkgs.overlays = [ (_: _: other-nixpkgs sysidentity.arch) (_: _: custom-packages-overlays) ] ++ package-overlays;
          })
        ]
        ++ (if lib.pathIsRegularFile ./machines/${dir}/configuration.nix
            then [ ./machines/${dir}/configuration.nix ]
            else [])
        ++ package-modules
        ++ modules;
      })
    (lib.filterAttrs (_: type: type == "directory") (builtins.readDir ./machines));
  packages = let
    systems = [ "x86_64-linux" "aarch64-linux" "aarch64-darwin" "x86_64-darwin" ];
  in
    lib.genAttrs systems (system:
      { nixpkgs = (import nixpkgs { inherit system; overlays = package-overlays; }); } //
      custom-packages system //
      other-nixpkgs system);
}