about summary refs log tree commit diff
path: root/flake.nix
blob: 030fbe214dd09c974d4da2a674564bac7b735723 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
{
  inputs = {
    nixpkgs.url = "git+ssh://git@github.com/nixos/nixpkgs.git?shallow=1&ref=nixos-24.11";
    nixpkgs2.url = "git+ssh://git@github.com/nixos/nixpkgs.git?shallow=1&ref=nixos-24.11";
    nixpkgs-unstable.url = "git+https://github.com/nixos/nixpkgs?ref=nixpkgs-unstable";

    # nix darwin version must match nixpkgs version:
    #   nixpkgs:    nixos-xx.yy
    #   nix-darwin: nix-darwin-xx.yy
    # with xx.yy being the same
    darwin.url = "git+https://github.com/lnl7/nix-darwin?ref=nix-darwin-24.11";
    darwin.inputs.nixpkgs.follows = "nixpkgs";

    deploy-rs.url = "git+https://github.com/serokell/deploy-rs?ref=master";
    deploy-rs.inputs.nixpkgs.follows = "nixpkgs";

    agenix.url = "git+https://github.com/ryantm/agenix";
    agenix.inputs.nixpkgs.follows = "nixpkgs";

    home-manager.url = "git+https://github.com/nix-community/home-manager?ref=release-24.11";
    home-manager.inputs.nixpkgs.follows = "nixpkgs";

    naersk.url = "git+https://github.com/nix-community/naersk";
    naersk.inputs.nixpkgs.follows = "nixpkgs";

    flake-utils.url = "git+https://github.com/numtide/flake-utils";

    # hefe-internal.url = "git+file:///Users/emile/hefe-internal";
    # hefe-internal.url = "git+ssh://git@git.emile.space/hefe-internal";

    # nix registry add flake:mylocalrepo git+file:///path/to/local/repo
    # nix registry list
    # hefe-internal.url = "flake:mylocalrepo";
  };

  outputs =
    {
      self,
      nixpkgs, # packages
      nixpkgs2, # packages2
      nixpkgs-unstable, # unstable branch
      darwin, # darwin related stuff
      deploy-rs, # deploy the hosts
      agenix, # store secrets crypted using age
      home-manager, # manage my home envs
      naersk, # build rust stuff
      flake-utils, # common flake utils
      # hefe-internal,    # internal tooling
      ...
    }@inputs:
    let
      lib = import ./nix/lib inputs;
      helper = lib.flake-helper;

      # TODO(emile): move all these functions into the helper, keeping the flake.nix clean

      # A function taking an attribute set of flake templates, importing their
      # flake.nix/output/packages (if there are any) and returning an attribute set
      # of their packages (if the template has one or more)
      template-packages =
        templ:
        (builtins.mapAttrs (
          name: value:
          (((import ./nix/templates/${name}/flake.nix).outputs) {
            inherit flake-utils;

            # need to provide nixpkgs WITHOUT the overlay for the packages defined in the template applied
            nixpkgs = nixpkgs;
          }).packages or { }
        ) templ);

      # apply the above function to the templates
      templates = template-packages self.templates;

      # Merge template packages into root packages with template prefix
      mergedTemplatePackages =
        system:
        let
          lib = nixpkgs.lib;
        in
        lib.foldl (
          acc: tplName:
          let
            tplPkgs = templates.${tplName}.${system} or { };
            prefixed = lib.mapAttrs' (pkgName: pkg: lib.nameValuePair "${tplName}-${pkgName}" pkg) tplPkgs;
          in
          acc // prefixed
        ) { } (builtins.attrNames templates);
    in
    {
      hosts = {
        caladan = {
          # in /etc/nix/machines
          # ssh://corrino.emile.space x86_64-linux - 16 2 nixos-test,benchmark,big-parallel,kvm - -
          system = "aarch64-darwin";
          sshUser = "hydra";
          homeManagerEnable = true;
          description = "macbook air";
          # nix run https://github.com/LnL7/nix-darwin/archive/master.tar.gz -- switch --flake .#caladan
        };

        # main vm host
        #
        # in case of broken config, reboot into recovery, then:
        #
        # cryptsetup luksOpen /dev/md1 luks0
        # mount /dev/disk/by-label/root /mnt
        # mkdir /mnt/boot
        # mount /dev/disk/by-label/root /mnt
        # grub-reboot --boot-directory /mnt/boot "Ni" <- press tab and choose wisely
        #
        # also see
        # //nix/hosts/corrino/hetzner-dedicated-wipe-and-install-nixos-luks-raid-lvm.sh
        corrino = {
          system = "x86_64-linux";
          ip = "corrino";
          description = "Hetzner AX41 dual 512GB NVME";
          modules = [
            # hefe-internal.nixosModules.corrino
            (
              { self, ... }:
              {
                nixpkgs.overlays = [
                  (final: prev: {
                    inherit (self.packages.x86_64-linux)
                      goapp-frontend
                      ;
                  })
                ];
              }
            )
          ];
        };
        chusuk = {
          # ip = "chusuk.pinto-pike.ts.net";
          system = "x86_64-linux";
          description = "lenovo t480";
        };
        hacknix = {
          # ip = "hacknix.pinto-pike.ts.net"; # clone repo and deploy within
          system = "x86_64-linux";
          description = "hacking vm";
        };
        mail = {
          # ip = "mail.pinto-pike.ts.net"; # clone repo and deploy within
          system = "x86_64-linux";
          description = "mail server";
        };

        #kolhar = {}; # nixos vm on caladan
        #hagal = {}; # apple tv

        lampadas = {
          system = "x86_64-linux"; # 4c4t (intel n100), 32GB RAM
          description = "NAS";
        };
        # palma = {
        #   description = "palma bmc";
        # };

        lernaeus = {
          system = "x86_64-linux"; # 8c16t (AMD ryzen 5 5600g), 32GB RAM
          description = "VM Host";
          ip = "192.168.1.79";
        };
        # parmentier = {
        #   description = "lernaeus bmc";
        # };

        # lankiveil = {
        #   system = "x86_64-linux"; # ???, ???, RTX A2000
        #   description = "Router";
        # };
        # poritrin = {
        #   description = "lankiveil bmc";
        # };

        # kaitain = {};
        # ecaz = {};
        # gamont = {};

        # futher names: https://neoencyclopedia.fandom.com/wiki/List_of_Dune_planets
        # Muritan
        # Naraj
        # Palma
        # Parmentier
        # Poritrin
        # Richese
        # Romo
        # Rossak
        # Sikun
        # Synchrony
        # Tleilax
        # Tupile
        # Zanovar
      };

      nixosConfigurations = helper.mapToNixosConfigurations self.hosts;
      darwinConfigurations = helper.mapToDarwinConfigurations self.hosts;

      nixosModules = {
        x86_64-linux = import ./nix/modules/x86_64-linux.nix;
        default = self.nixosModules.x86_64-linux;
      };

      overlays = {
        default = self.overlays.x86_64-linux;

        x86_64-linux = import ./nix/pkgs/x86_64-linux.nix;
        aarch64-darwin = import ./nix/pkgs/aarch64-darwin.nix;

        unstable = final: prev: {
          unstable = import nixpkgs-unstable {
            system = "x86_64-linux";
            config.allowUnfree = true;
          };
        };

        # no clue why, but when rebuilding corrino and this not being commented,
        # something in the hardware.bluetooth module breaks
        #
        # unstable-darwin = final: prev: {
        #   unstable-darwin = import nixpkgs-unstable {
        #     system = "aarch64-darwin";
        #     config.allowUnfree = true;
        #   };
        # };
      };

      deploy.nodes = helper.mapToDeployRsConfiguration self.hosts;
      deploy.autoRollback = true;

      packages =
        nixpkgs.lib.genAttrs
          [
            "x86_64-linux"
            "aarch64-darwin"
          ]
          (
            system:
            let
              pkgs = import nixpkgs {
                inherit system;
                overlays = [
                  (
                    if system == "x86_64-linux" then
                      self.overlays.x86_64-linux
                    else if system == "aarch64-darwin" then
                      self.overlays.aarch64-darwin
                    else
                      null
                  )
                  # some arguments for packages
                  (_: _: { inherit naersk; })
                ];
              };
            in
            # take all the packages exposed from templates and add them to
            # the packages exposed by this flake
            mergedTemplatePackages system
            // {
              inherit (pkgs) vokobe r2wars-web remarvin;
            }
          );

      hydraJobs = {
        inherit (self) packages;
        nixosConfigurations = helper.buildHosts self.nixosConfigurations;
        templates = template-packages self.templates;
      };

      templates = {
        # ; nix nix registry add hefe /Users/emile/Documents/hefe
        # ; nix flake init -t hefe#ctf
        ctf = {
          description = "A basic ctf env with pwn, rev, ... tools";
          path = ./nix/templates/ctf;
          welcomeText = ''
            # A basic CTF env
            ## Intended usage
            The intended usage of this flake is...

            ## More info
            - [Rust language](https://www.rust-lang.org/)
            - [Rust on the NixOS Wiki](https://nixos.wiki/wiki/Rust)
            - ...
          '';
        };
        goapp = {
          description = "A basic golang service";
          path = ./nix/templates/goapp;
          welcomeText = ''
            # A basic golang service
             
            - using gorilla/mux
          '';
        };

        # checks = builtins.mapAttrs (system: deployLib:
        #     deployLib.deployChecks self.deploy) deploy-rs.lib;
      };
    };
}