From 0d399b63b9ae014bc30ca9b67c81898018947164 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Thu, 9 May 2024 00:59:02 +0200 Subject: [PATCH] Allow configuring user homes --- flake.nix | 95 +++++++++++++++++++++++++++++++++++++++++----------- lib/home.nix | 2 ++ 2 files changed, 77 insertions(+), 20 deletions(-) create mode 100644 lib/home.nix diff --git a/flake.nix b/flake.nix index 2513686..c624e9f 100644 --- a/flake.nix +++ b/flake.nix @@ -10,10 +10,30 @@ outputs = { self, nixpkgs, flake-utils, sops-nix }: ( let inherit (nixpkgs) lib; + + defaultMachine = { + system = "x86_64-linux"; + + config = { pkgs, ... }: { + dualBoot = false; + timeZone = null; + keyMap = "us"; + keyboardLayout = "us"; + localeSettings = { }; + users = { }; + }; + }; + + machines = { + nixos.config = { ... }: { + }; + }; + + systems = builtins.mapAttrs (name: value: defaultMachine // value) machines; tryFiles = files: default: lib.lists.foldr (a: b: - if builtins.pathExists a + if (a != null) && (builtins.pathExists a) then a else b) default @@ -54,22 +74,8 @@ } ) // { nixosConfigurations = - let - default = { pkgs, ... }: { - dualBoot = false; - timeZone = null; - keyMap = "us"; - keyboardLayout = "us"; - localeSettings = { }; - users = { }; - }; - systems = { - nixos.config = { ... }: { - }; - }; - in builtins.mapAttrs ( - name: { system ? "x86_64-linux", config }: nixpkgs.lib.nixosSystem { + name: { system, config }: nixpkgs.lib.nixosSystem { inherit system; modules = [ @@ -78,7 +84,7 @@ config._module.args = { hostname = name; - machineConfig = (default args) // + machineConfig = (defaultMachine.config args) // (config args) // { inherit name; }; @@ -88,7 +94,56 @@ ./lib/configuration.nix (tryFiles [ ./lib/machines/${name}.nix ] ./lib/hardware/base.nix) ]; - }) - systems; - }); + }) + systems; + + homeConfigurations = + let + lib = nixpkgs.lib; + in + lib.attrsets.concatMapAttrs ( + hostname: machineDeclaration: + let + machine = machineDeclaration // defaultMachine // { + config = { ... }@args: ( + (defaultMachine.config args) // + (machineDeclaration.config args)); + }; + + pkgs = import nixpkgs { + inherit (machine) system; + }; + in + lib.attrsets.concatMapAttrs ( + username: user: { + "${username}@${hostname}" = { + modules = + let + userConfigPath = ( + tryFiles [ + ./lib/users/${username}/${hostname}.nix + ./lib/users/${username}/common.nix + ./lib/users/${username}.nix + ] + null); + in [ + ( + { pkgs, ... }: { + config._module.args = { + userConfig = { + inherit + hostname + username + ; + } // user; + }; + }) + sops-nix.homeManagerModules.sops + ./lib/home.nix + ] ++ (lib.optional (userConfigPath != null) userConfigPath); + }; + }) + (machine.config { inherit pkgs; }).users) + machines; + }); } diff --git a/lib/home.nix b/lib/home.nix new file mode 100644 index 0000000..4b2c5c4 --- /dev/null +++ b/lib/home.nix @@ -0,0 +1,2 @@ +{ ... }: { +}