From b9f3d8f46ca93e64280c7798c20309781f869b91 Mon Sep 17 00:00:00 2001
From: Julien Malka <julien.malka@me.com>
Date: Sat, 19 Feb 2022 22:05:59 +0100
Subject: [PATCH] Refactoring of the machines

---
 flake.lock                             | 39 ++++++++++++++++++++++-
 flake.nix                              | 22 +++++++------
 lib/default.nix                        |  2 ++
 lib/luj.nix                            | 43 ++++++++++++++++++++++++++
 rpi.nix => machines/lambda/default.nix |  2 +-
 5 files changed, 97 insertions(+), 11 deletions(-)
 create mode 100644 lib/luj.nix
 rename rpi.nix => machines/lambda/default.nix (98%)

diff --git a/flake.lock b/flake.lock
index 05a13a0..417f6b4 100644
--- a/flake.lock
+++ b/flake.lock
@@ -16,6 +16,27 @@
         "type": "gitlab"
       }
     },
+    "dns": {
+      "inputs": {
+        "flake-utils": "flake-utils",
+        "nixpkgs": [
+          "nixpkgs"
+        ]
+      },
+      "locked": {
+        "lastModified": 1635273082,
+        "narHash": "sha256-EHiDP2jEa7Ai5ZwIf5uld9RVFcV77+2SUxjQXwJsJa0=",
+        "owner": "kirelagin",
+        "repo": "dns.nix",
+        "rev": "c7b9645da9c0ddce4f9de4ef27ec01bb8108039a",
+        "type": "github"
+      },
+      "original": {
+        "owner": "kirelagin",
+        "repo": "dns.nix",
+        "type": "github"
+      }
+    },
     "flake-compat": {
       "flake": false,
       "locked": {
@@ -33,6 +54,21 @@
       }
     },
     "flake-utils": {
+      "locked": {
+        "lastModified": 1614513358,
+        "narHash": "sha256-LakhOx3S1dRjnh0b5Dg3mbZyH0ToC9I8Y2wKSkBaTzU=",
+        "owner": "numtide",
+        "repo": "flake-utils",
+        "rev": "5466c5bbece17adaab2d82fae80b46e807611bf3",
+        "type": "github"
+      },
+      "original": {
+        "owner": "numtide",
+        "repo": "flake-utils",
+        "type": "github"
+      }
+    },
+    "flake-utils_2": {
       "locked": {
         "lastModified": 1629481132,
         "narHash": "sha256-JHgasjPR0/J1J3DRm4KxM4zTyAj4IOJY8vIl75v/kPI=",
@@ -85,7 +121,7 @@
     },
     "neovim-flake": {
       "inputs": {
-        "flake-utils": "flake-utils",
+        "flake-utils": "flake-utils_2",
         "nixpkgs": "nixpkgs"
       },
       "locked": {
@@ -204,6 +240,7 @@
     },
     "root": {
       "inputs": {
+        "dns": "dns",
         "home-manager": "home-manager",
         "homepage": "homepage",
         "neovim-nightly-overlay": "neovim-nightly-overlay",
diff --git a/flake.nix b/flake.nix
index 79dbcf8..955df9f 100644
--- a/flake.nix
+++ b/flake.nix
@@ -8,6 +8,11 @@
       inputs.nixpkgs.follows = "nixpkgs";
     };
 
+    dns = {
+      url = "github:kirelagin/dns.nix";
+      inputs.nixpkgs.follows = "nixpkgs";
+    };
+
     neovim-nightly-overlay = {
       url = "github:nix-community/neovim-nightly-overlay";
       inputs.nixpkgs.follows = "unstable";
@@ -36,7 +41,7 @@
   outputs = { self, home-manager, nixpkgs, unstable, sops-nix, neovim-nightly-overlay, nur, ... }@inputs:
     let
       pkgs = import nixpkgs { system = "x86_64-linux"; };
-      pkgsrpi = import nixpkgs { system = "aarch64-linux";};
+      pkgsrpi = import nixpkgs { system = "aarch64-linux"; };
       lib = nixpkgs.lib.extend (import ./lib inputs);
     in
     with lib;
@@ -48,19 +53,18 @@
         })
         (builtins.attrNames (builtins.readDir ./modules)));
 
-        nixosConfigurations = builtins.mapAttrs (name: value: (mkMachine {host=name; host-config=value; modules=self.nixosModules;})) (importConfig ./machines) //
-        {"lambda" = mkMachine { host = "lambda"; host-config = import ./rpi.nix; modules=self.nixosModules; system="aarch64-linux";};};
+      nixosConfigurations = builtins.mapAttrs (name: value: (mkMachine { host = name; host-config = value; modules = self.nixosModules; system = luj.machines.${name}.arch; })) (importConfig ./machines);
       packages."x86_64-linux" = {
         tinystatus = import ./packages/tinystatus { inherit pkgs; };
-        mosh = pkgs.callPackage ./packages/mosh {};
-        flaresolverr = pkgs.callPackage ./packages/flaresolverr {};
-        htpdate = pkgs.callPackage ./packages/htpdate {};
+        mosh = pkgs.callPackage ./packages/mosh { };
+        flaresolverr = pkgs.callPackage ./packages/flaresolverr { };
+        htpdate = pkgs.callPackage ./packages/htpdate { };
       };
       packages."aarch64-linux" = {
         tinystatus = import ./packages/tinystatus { pkgs = pkgsrpi; };
-        mosh = pkgsrpi.callPackage ./packages/mosh {};
-        flaresolverr = pkgsrpi.callPackage ./packages/flaresolverr {};
-        htpdate = pkgsrpi.callPackage ./packages/htpdate {};
+        mosh = pkgsrpi.callPackage ./packages/mosh { };
+        flaresolverr = pkgsrpi.callPackage ./packages/flaresolverr { };
+        htpdate = pkgsrpi.callPackage ./packages/htpdate { };
       };
     };
 }
diff --git a/lib/default.nix b/lib/default.nix
index 2f6627e..170181c 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -52,5 +52,7 @@ in
     };
   };
 
+  luj = import ./luj.nix final;
+
 }
 
diff --git a/lib/luj.nix b/lib/luj.nix
new file mode 100644
index 0000000..a8716ba
--- /dev/null
+++ b/lib/luj.nix
@@ -0,0 +1,43 @@
+lib: with lib; let
+  modules = [
+    {
+      options.machines = mkOption {
+        description = "My machines";
+        type = with types; attrsOf (submodule ({ name, ... }: {
+          freeformType = attrs;
+          options = {
+            hostname = mkOption {
+              description = "The machine's hostname";
+              type = str;
+              default = name;
+              readOnly = true;
+            };
+          };
+        }));
+        default = {};
+      };
+
+      config = {
+        _module.freeformType = with types; attrs;
+
+        domain = "julienmalka.me";
+
+        machines = {
+          lisa = {
+            arch = "x86_64-linux";
+          };
+          newton = {
+            arch = "x86_64-linux";
+          };
+          macintosh = {
+            arch = "x86_64-linux";
+          };
+          lambda = {
+            arch = "aarch64-linux";
+          };
+        };
+      };
+    }
+  ];
+in (evalModules { inherit modules; }).config
+
diff --git a/rpi.nix b/machines/lambda/default.nix
similarity index 98%
rename from rpi.nix
rename to machines/lambda/default.nix
index f66c21c..1e8439a 100644
--- a/rpi.nix
+++ b/machines/lambda/default.nix
@@ -2,7 +2,7 @@
 {
 
 
-  imports = [ ./users/status.nix ];
+  imports = [ ../../users/status.nix ];
   # NixOS wants to enable GRUB by default
   boot.loader.grub.enable = false;