From 51d961484e036b21bf3270c6fcb66c00bca6462a Mon Sep 17 00:00:00 2001 From: Giulio De Pasquale Date: Tue, 14 Feb 2023 21:14:30 +0100 Subject: [PATCH] lib/ cleanup, introduced utils to reduce code duplication --- flake.nix | 5 ++++- lib/host.nix | 17 ++++++++--------- lib/user.nix | 14 ++++++-------- lib/utils.nix | 6 ++++++ 4 files changed, 24 insertions(+), 18 deletions(-) create mode 100644 lib/utils.nix diff --git a/flake.nix b/flake.nix index ef01d73..9ddce17 100644 --- a/flake.nix +++ b/flake.nix @@ -36,8 +36,11 @@ inherit (pkgs.lib) makeScope; inherit (pkgs) newScope; in - makeScope newScope (self: { + makeScope newScope (self: rec { inherit nixpkgs home-manager nixos-unstable; + inherit (self.callPackage ./lib/utils.nix { }) mkSysRole mkHomeRole; + inherit (user) mkUser; + user = self.callPackage ./lib/user.nix { }; host = self.callPackage ./lib/host.nix { }; }); diff --git a/lib/host.nix b/lib/host.nix index e28b753..72d4e48 100644 --- a/lib/host.nix +++ b/lib/host.nix @@ -1,18 +1,16 @@ -{ pkgs, nixpkgs, nixos-unstable, unstable, home-manager, user, system, ... }: +{ pkgs, nixpkgs, nixos-unstable, unstable, home-manager, system, mkHomeRole, mkSysRole, mkUser, ... }: { mkHost = { name, users, roles ? [ ], imports ? [ ] }: let - mkRole = role: pkgs.callPackage (../roles + "/${role}.nix") { }; - users_mod = (map (u: - user.mkUser { + mkUser { name = u.user; roles = u.roles; }) users); - roles_mod = (map (r: mkRole r) roles); + roles_mod = (map (r: mkSysRole r) roles); add_imports = imports; in nixpkgs.lib.nixosSystem { @@ -20,7 +18,10 @@ modules = [ { - imports = users_mod ++ roles_mod ++ add_imports; + imports = users_mod ++ roles_mod ++ add_imports ++ [ + (mkSysRole "common") + (mkSysRole "acme") + ]; nixpkgs = { inherit pkgs; }; nix.nixPath = [ "nixpkgs=${nixpkgs}" "unstable=${nixos-unstable}" ]; @@ -30,7 +31,7 @@ users.users.root = { shell = pkgs.zsh; }; home-manager = { - users.root.imports = [ ../roles/home/common.nix ]; + users.root.imports = [ (mkHomeRole "common") ]; extraSpecialArgs.unstable = unstable; useGlobalPkgs = true; }; @@ -38,8 +39,6 @@ } home-manager.nixosModules.home-manager - ../roles/common.nix - ../roles/acme.nix ../hosts/${name}/default.nix ]; }; diff --git a/lib/user.nix b/lib/user.nix index 16d4852..73afbff 100644 --- a/lib/user.nix +++ b/lib/user.nix @@ -1,10 +1,9 @@ -{ pkgs, unstable, home-manager, ... }: +{ pkgs, unstable, home-manager, mkHomeRole, ... }: { mkUser = { name, roles ? [ ] }: let - mkRole = role: import (../roles/home + "/${role}.nix"); - roles_mod = (map (r: mkRole r) roles); + roles_mod = (map (r: mkHomeRole r) roles); in { users.groups.plugdev = { }; @@ -21,13 +20,12 @@ extraGroups = [ "wheel" "plugdev" ]; }; - home-manager.users.${name}.imports = [ (mkRole "common") ] ++ roles_mod; + home-manager.users.${name}.imports = [ (mkHomeRole "common") ] ++ roles_mod; }; - mkHMUser = { name, roles }: + mkHMUser = { name, roles ? [ ] }: let - mkRole = role: import (../roles/home + "/${role}.nix"); - roles_mod = (map (r: mkRole r) roles); + roles_mod = (map (r: mkHomeRole r) roles); in home-manager.lib.homeManagerConfiguration { inherit pkgs; @@ -39,7 +37,7 @@ if pkgs.stdenv.isLinux then "/home/${name}" else "/Users/${name}"; }; } - (mkRole "common") + (mkHomeRole "common") ] ++ roles_mod; }; } diff --git a/lib/utils.nix b/lib/utils.nix new file mode 100644 index 0000000..7584428 --- /dev/null +++ b/lib/utils.nix @@ -0,0 +1,6 @@ +{ ... }: + +{ + mkSysRole = role: import (../roles/${role}.nix); + mkHomeRole = role: import (../roles/home/${role}.nix); +}