From c69c7c9b1114089f5f93a408d71b802b5c97b72a Mon Sep 17 00:00:00 2001 From: DannyDannyDanny Date: Sat, 18 Apr 2026 17:20:51 +0200 Subject: [PATCH] =?UTF-8?q?refactor(nix):=20dedupe=20home-manager=20wiring?= =?UTF-8?q?=20across=20hosts=20=E2=99=BB=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Extract the per-host home-manager block (useGlobalPkgs, useUserPackages, backupFileExtension, users. with username/homeDirectory/optional stateVersion/optional imports) into nixos/lib/home-manager-user.nix. Each flake-module now imports it with its per-host parameters, removing ~40 lines of boilerplate across the four hosts. --- nixos/flake-modules/daniel-macbook-air.nix | 19 ++++-------- nixos/flake-modules/phantom-ship.nix | 15 ++++------ nixos/flake-modules/sunken-ship.nix | 15 ++++------ nixos/flake-modules/wsl.nix | 15 ++++------ nixos/lib/home-manager-user.nix | 35 ++++++++++++++++++++++ 5 files changed, 55 insertions(+), 44 deletions(-) create mode 100644 nixos/lib/home-manager-user.nix diff --git a/nixos/flake-modules/daniel-macbook-air.nix b/nixos/flake-modules/daniel-macbook-air.nix index 68ede41..4729175 100644 --- a/nixos/flake-modules/daniel-macbook-air.nix +++ b/nixos/flake-modules/daniel-macbook-air.nix @@ -10,21 +10,12 @@ ../hosts/daniel-macbook-air.nix ../fish.nix - # Home Manager on macOS inputs.home-manager.darwinModules.home-manager - ({ lib, ... }: { - home-manager.useGlobalPkgs = true; - home-manager.useUserPackages = true; - # Automatically backup files before home-manager overwrites them - home-manager.backupFileExtension = "backup"; - home-manager.users.danny = { ... }: { - # Force an absolute path even if another module sets a bad value. - home.username = "danny"; - home.homeDirectory = lib.mkForce "/Users/danny"; - imports = [ - ../home/danny/home.nix - ]; - }; + (import ../lib/home-manager-user.nix { + lib = inputs.nixpkgs.lib; + user = "danny"; + homeDirectory = "/Users/danny"; + userImports = [ ../home/danny/home.nix ]; }) ]; }; diff --git a/nixos/flake-modules/phantom-ship.nix b/nixos/flake-modules/phantom-ship.nix index 24d1d2d..f77b454 100644 --- a/nixos/flake-modules/phantom-ship.nix +++ b/nixos/flake-modules/phantom-ship.nix @@ -5,17 +5,12 @@ inputs.nix-openclaw.nixosModules.openclaw-gateway ../hosts/phantom-ship.nix - # Home Manager on NixOS inputs.home-manager.nixosModules.home-manager - ({ lib, ... }: { - home-manager.useGlobalPkgs = true; - home-manager.useUserPackages = true; - home-manager.backupFileExtension = "backup"; - home-manager.users.danny = { ... }: { - home.username = "danny"; - home.homeDirectory = lib.mkForce "/home/danny"; - home.stateVersion = "25.11"; - }; + (import ../lib/home-manager-user.nix { + lib = inputs.nixpkgs.lib; + user = "danny"; + homeDirectory = "/home/danny"; + stateVersion = "25.11"; }) ]; }; diff --git a/nixos/flake-modules/sunken-ship.nix b/nixos/flake-modules/sunken-ship.nix index 3a78476..402a126 100644 --- a/nixos/flake-modules/sunken-ship.nix +++ b/nixos/flake-modules/sunken-ship.nix @@ -4,17 +4,12 @@ modules = [ ../hosts/sunken-ship.nix - # Home Manager on NixOS inputs.home-manager.nixosModules.home-manager - ({ lib, ... }: { - home-manager.useGlobalPkgs = true; - home-manager.useUserPackages = true; - home-manager.backupFileExtension = "backup"; - home-manager.users.danny = { ... }: { - home.username = "danny"; - home.homeDirectory = lib.mkForce "/home/danny"; - home.stateVersion = "25.11"; - }; + (import ../lib/home-manager-user.nix { + lib = inputs.nixpkgs.lib; + user = "danny"; + homeDirectory = "/home/danny"; + stateVersion = "25.11"; }) ]; }; diff --git a/nixos/flake-modules/wsl.nix b/nixos/flake-modules/wsl.nix index 711a66e..e9a491d 100644 --- a/nixos/flake-modules/wsl.nix +++ b/nixos/flake-modules/wsl.nix @@ -7,17 +7,12 @@ ../hosts/wsl.nix ../fish.nix - # Home Manager on WSL inputs.home-manager.nixosModules.home-manager - ({ lib, ... }: { - home-manager.useGlobalPkgs = true; - home-manager.useUserPackages = true; - home-manager.backupFileExtension = "backup"; - home-manager.users.dth = { ... }: { - home.username = "dth"; - home.homeDirectory = lib.mkForce "/home/dth"; - imports = [ ../home/danny/home.nix ]; - }; + (import ../lib/home-manager-user.nix { + lib = inputs.nixpkgs.lib; + user = "dth"; + homeDirectory = "/home/dth"; + userImports = [ ../home/danny/home.nix ]; }) ]; }; diff --git a/nixos/lib/home-manager-user.nix b/nixos/lib/home-manager-user.nix new file mode 100644 index 0000000..7ee81e0 --- /dev/null +++ b/nixos/lib/home-manager-user.nix @@ -0,0 +1,35 @@ +# Shared home-manager wiring for NixOS and nix-darwin hosts. +# +# Usage (from a flake-module): +# modules = [ +# inputs.home-manager.nixosModules.home-manager # or .darwinModules +# (import ../lib/home-manager-user.nix { +# lib = inputs.nixpkgs.lib; +# user = "danny"; +# homeDirectory = "/home/danny"; +# stateVersion = "25.11"; # optional +# userImports = [ ../home/danny/home.nix ]; # optional +# }) +# ]; +{ lib +, user +, homeDirectory +, stateVersion ? null +, userImports ? [ ] +}: +{ + home-manager.useGlobalPkgs = true; + home-manager.useUserPackages = true; + # Automatically back up files before home-manager overwrites them. + home-manager.backupFileExtension = "backup"; + home-manager.users.${user} = { ... }: { + imports = userImports; + home = { + username = user; + # Force an absolute path even if another module sets a bad value. + homeDirectory = lib.mkForce homeDirectory; + } // lib.optionalAttrs (stateVersion != null) { + stateVersion = stateVersion; + }; + }; +}