From ba5fd48569a37e6ec791c78464aab342916d9ac2 Mon Sep 17 00:00:00 2001 From: Julia Lange Date: Thu, 14 Nov 2024 13:55:47 -0800 Subject: [PATCH] Hosts, Use directory-driven hosts in flake.nix This adds some code to flake.nix to read the directory of hosts/ and find all hosts based on their host.nix file, and set the hostname based on the directory. I don't know enough about hostnames to know what are valid characters, but in the future this could allow a hostname such as "servers/madoka" which may be an issue. For now the structure is simple enough of "hosts/name" resulting in hostname "name" Another commit is planned which will do the same thing for users --- flake.nix | 20 +++++++++++++++----- hosts/jibril/{default.nix => host.nix} | 0 hosts/onizuka/{default.nix => host.nix} | 0 3 files changed, 15 insertions(+), 5 deletions(-) rename hosts/jibril/{default.nix => host.nix} (100%) rename hosts/onizuka/{default.nix => host.nix} (100%) diff --git a/flake.nix b/flake.nix index 85c6a1a..7644915 100644 --- a/flake.nix +++ b/flake.nix @@ -15,6 +15,8 @@ outputs = { self, home-manager, nixpkgs, ... }@inputs: let system = "x86_64-linux"; pkgs = nixpkgs.legacyPackages.${system}; + fs = pkgs.lib.fileset; + st = pkgs.lib.strings; hostConfig = extraModules: nixpkgs.lib.nixosSystem { specialArgs = { inherit inputs; }; @@ -23,6 +25,15 @@ ./nixosModules ] ++ extraModules; }; + hostFilter = { name, ...}: name == "host.nix"; + hostPaths = fs.toList (fs.fileFilter hostFilter ./hosts); + + hosts = builtins.listToAttrs (map (path: { + value = hostConfig [ path ]; + name = builtins.unsafeDiscardStringContext (st.removeSuffix "/host.nix" ( + builtins.elemAt (st.splitString "/hosts/" path) 1 + )); + }) hostPaths); userConfig = extraModules: home-manager.lib.homeManagerConfiguration { inherit pkgs; @@ -31,12 +42,11 @@ ./hmModules ] ++ extraModules; }; - in - { - nixosConfigurations.onizuka = hostConfig [ ./hosts/onizuka ]; - homeConfigurations."pan@onizuka" = userConfig [ ./hosts/onizuka/users/pan ]; - nixosConfigurations.jibril = hostConfig [ ./hosts/jibril ]; + in { + nixosConfigurations = hosts; + + homeConfigurations."pan@onizuka" = userConfig [ ./hosts/onizuka/users/pan ]; homeConfigurations."pan@jibril" = userConfig [ ./hosts/jibril/users/pan ]; }; } diff --git a/hosts/jibril/default.nix b/hosts/jibril/host.nix similarity index 100% rename from hosts/jibril/default.nix rename to hosts/jibril/host.nix diff --git a/hosts/onizuka/default.nix b/hosts/onizuka/host.nix similarity index 100% rename from hosts/onizuka/default.nix rename to hosts/onizuka/host.nix