From 1ea937f884db85390c6d286bbf900d3dd93cc869 Mon Sep 17 00:00:00 2001 From: caem Date: Thu, 25 Jul 2024 21:24:28 +0200 Subject: [PATCH] Refactor: Initial step - Added library for common functions - Moved machines/ to hosts/ - Automise adding new hosts to the flake --- flake.nix | 13 ++++++---- .../workstation/default.nix | 0 .../workstation/hardware-configuration.nix | 0 {machines => hosts}/workstation/persist.nix | 0 lib/default.nix | 10 +++++++ lib/fs.nix | 10 +++++++ lib/hosts.nix | 26 +++++++++++++++++++ 7 files changed, 54 insertions(+), 5 deletions(-) rename machines/workstation/configuration.nix => hosts/workstation/default.nix (100%) rename {machines => hosts}/workstation/hardware-configuration.nix (100%) rename {machines => hosts}/workstation/persist.nix (100%) create mode 100644 lib/default.nix create mode 100644 lib/fs.nix create mode 100644 lib/hosts.nix diff --git a/flake.nix b/flake.nix index b6138bf..a320136 100644 --- a/flake.nix +++ b/flake.nix @@ -22,10 +22,13 @@ }; outputs = { nixpkgs, impermanence, home-manager, aagl, nur, ... } @ inputs: + let + lib = nixpkgs.lib.extend (final: prev: + import ./lib { lib = final; } + ); + in { - nixosConfigurations.workstation = nixpkgs.lib.nixosSystem { - system = "x86_64-linux"; - specialArgs = { inherit inputs; } ; + nixosConfigurations = lib.mkHosts { modules = [ home-manager.nixosModules.home-manager { home-manager.sharedModules = [ @@ -35,9 +38,9 @@ impermanence.nixosModules.impermanence nur.nixosModules.nur aagl.nixosModules.default - - ./machines/workstation/configuration.nix ]; + nixpkgs = nixpkgs; + inputs = inputs; }; }; } diff --git a/machines/workstation/configuration.nix b/hosts/workstation/default.nix similarity index 100% rename from machines/workstation/configuration.nix rename to hosts/workstation/default.nix diff --git a/machines/workstation/hardware-configuration.nix b/hosts/workstation/hardware-configuration.nix similarity index 100% rename from machines/workstation/hardware-configuration.nix rename to hosts/workstation/hardware-configuration.nix diff --git a/machines/workstation/persist.nix b/hosts/workstation/persist.nix similarity index 100% rename from machines/workstation/persist.nix rename to hosts/workstation/persist.nix diff --git a/lib/default.nix b/lib/default.nix new file mode 100644 index 0000000..c62cc11 --- /dev/null +++ b/lib/default.nix @@ -0,0 +1,10 @@ +{ lib }: + +# todo: Write a function to import all of these automatically + +let + fs = import ./fs.nix { inherit lib; }; + hosts = import ./hosts.nix { inherit lib; }; +in + fs // hosts + diff --git a/lib/fs.nix b/lib/fs.nix new file mode 100644 index 0000000..cb8d639 --- /dev/null +++ b/lib/fs.nix @@ -0,0 +1,10 @@ +{ lib }: + +{ + getDirsInDir = + path: let + dirs = builtins.readDir path; + in + builtins.filter (name: dirs.${name} == "directory") (builtins.attrNames dirs); +} + diff --git a/lib/hosts.nix b/lib/hosts.nix new file mode 100644 index 0000000..4460eb9 --- /dev/null +++ b/lib/hosts.nix @@ -0,0 +1,26 @@ +{ lib }: + +{ + mkHosts = { + modules, + nixpkgs, + inputs, + path ? (builtins.toString ../hosts), + }: let + hosts = lib.getDirsInDir path; + common = { + system = "x86_64-linux"; + modules = modules; + }; + in + builtins.trace "Hosts found: ${builtins.toString hosts}" + builtins.trace "Path being used: ${path}" + builtins.listToAttrs (builtins.map (host: { + name = host; + value = nixpkgs.lib.nixosSystem { + system = common.system; + modules = common.modules ++ [ ../hosts/${host} ]; + specialArgs = { inherit inputs; }; + }; + }) hosts); +}