summaryrefslogtreecommitdiff
path: root/flake.nix
blob: 703ed9c2800adb41f774342c01732e7d36aedf62 (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
{
  description = "Personal monorepo";
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";

    disko.url = "github:nix-community/disko/v1.6.1";
    disko.inputs.nixpkgs.follows = "nixpkgs";

    nixos-hardware.url = "github:nixos/nixos-hardware/master";

    # Rust projects
    naersk.url = "github:nix-community/naersk";
    naersk.inputs.nixpkgs.follows = "nixpkgs";
  };

  outputs = { self, nixpkgs, ... } @ inputs:
    let
      inherit (builtins)
        attrNames
        listToAttrs
        map
        readDir
        ;

      inherit (nixpkgs) lib;

      inherit (nixpkgs.lib)
        mapAttrs
        nixosSystem
        ;

      systems = [ "x86_64-linux" "aarch64-linux" ];

      inherit (import ./lib/depot-tools.nix { inherit inputs systems lib; })
        importDir
        eachSystem
        systemArgs
        ;

      packages = eachSystem (
        { newScope, ... }:
        mapAttrs (pname: { path, ... }: newScope { inherit pname; } path { })
          (importDir ./packages)
      );

      publisherArgs = {
        flake = self;
        inherit inputs;
      };

      expectsPublisherArgs =
        module:
        builtins.isFunction module
        && builtins.all (arg: builtins.elem arg (builtins.attrNames publisherArgs)) (
          builtins.attrNames (builtins.functionArgs module)
        );

      injectPublisherArgs =
        modulePath:
        let
          module = import modulePath;
        in
        if expectsPublisherArgs module then
          lib.setDefaultModuleLocation modulePath (module publisherArgs)
        else
          modulePath;

      nixosModules = mapAttrs (_: moduleDir: injectPublisherArgs moduleDir)
        (mapAttrs (_: { path, ... }: path) (importDir ./modules));
    in
    {
      inherit packages nixosModules;

      nixosConfigurations = listToAttrs (
        map
          (name: {
            inherit name;
            value = nixosSystem rec {
              system = "x86_64-linux";
              specialArgs = {
                inherit inputs;
                inherit (self) outputs;
                inherit (systemArgs.${system}) flake perSystem;
              };
              modules = [
                # Machine's specific configuration
                ./machines/${name}

                # Implicitly import xnet
                self.nixosModules.xnet

                # Global module
                ({ inputs, ... }: {

                  # Set system nixpkgs to flake input
                  nix.registry.nixpkgs.flake = inputs.nixpkgs;
                })
              ];
            };
          })
          (attrNames (readDir ./machines))
      );
    };
}