From 4d6fdba39012848c267ce58aa59a315fb2623dbc Mon Sep 17 00:00:00 2001 From: Jake Hamilton Date: Mon, 10 Apr 2023 16:50:47 -0700 Subject: [PATCH] wip: better home-manager integration --- flake.nix | 4 +++ lib/home/default.nix | 48 ++++++++++++++++++++------------- modules/darwin/home/default.nix | 33 ----------------------- modules/home/os/default.nix | 28 ------------------- modules/home/user/default.nix | 23 ++++++++++++++++ modules/nixos/home/default.nix | 33 ----------------------- 6 files changed, 57 insertions(+), 112 deletions(-) delete mode 100644 modules/darwin/home/default.nix delete mode 100644 modules/home/os/default.nix create mode 100644 modules/home/user/default.nix delete mode 100644 modules/nixos/home/default.nix diff --git a/flake.nix b/flake.nix index 49ba302..a023d2a 100644 --- a/flake.nix +++ b/flake.nix @@ -38,5 +38,9 @@ in { inherit mkLib mkFlake; + + homeModules = { + user = ./user/default.nix; + }; }; } diff --git a/lib/home/default.nix b/lib/home/default.nix index a956e39..d4e0ad3 100644 --- a/lib/home/default.nix +++ b/lib/home/default.nix @@ -1,7 +1,7 @@ { core-inputs, user-inputs, snowfall-lib }: let - inherit (core-inputs.nixpkgs.lib) assertMsg foldl head tail concatMap optionalAttrs mkIf filterAttrs mapAttrs' mkMerge mapAttrsToList; + inherit (core-inputs.nixpkgs.lib) assertMsg foldl head tail concatMap optionalAttrs mkIf filterAttrs mapAttrs' mkMerge mapAttrsToList optionals mkDefault; user-homes-root = snowfall-lib.fs.get-snowfall-file "homes"; user-modules-root = snowfall-lib.fs.get-snowfall-file "modules"; @@ -157,7 +157,6 @@ in inherit system target format virtual systems host; lib = home-lib; - # pkgs = user-inputs.self.pkgs.${system}.nixpkgs; inputs = snowfall-lib.flake.without-src user-inputs; }; @@ -179,30 +178,43 @@ in let host-matches = created-user.specialArgs.host == host; - wrapped-user-module = home-args: + # @NOTE(jakehamilton): We *must* specify named attributes here in order + # for home-manager to provide them. + wrapped-user-module = home-args@{ pkgs, lib, ... }: let - modified-args = args // { - inherit (created-user.specialArgs) user; - }; - user-module-result = (import user-module (home-args // modified-args)) // { - _file = user-module; - }; + user-module-result = import user-module home-args; + user-imports = + if user-module-result ? imports then + user-module-result.imports + else + [ ]; + user-config = + if user-module-result ? config then + user-module-result.config + else + builtins.removeAttrs user-module-result [ "imports" "options" "_file" ]; in - user-module-result; + { + _file = builtins.toString user-module; + imports = user-imports; + + config = mkMerge [ + user-config + ({ + snowfallorg.user.name = mkDefault created-user.specialArgs.user; + }) + ]; + }; in { _file = "virtual:snowfallorg/home/user/${name}"; - imports = - if snowfall-lib.system.is-darwin created-user.system then - [ ../../modules/darwin/home/default.nix ] - else - [ ../../modules/nixos/home/default.nix ]; - config = mkIf host-matches { home-manager = { - users.${user-name} = wrapped-user-module args; - sharedModules = other-modules; + users.${user-name} = wrapped-user-module; + sharedModules = other-modules ++ [ + ../../modules/home/user/default.nix + ]; }; }; } diff --git a/modules/darwin/home/default.nix b/modules/darwin/home/default.nix deleted file mode 100644 index e9cd0a8..0000000 --- a/modules/darwin/home/default.nix +++ /dev/null @@ -1,33 +0,0 @@ -{ lib, config, ... }: - -let - inherit (lib) types mkOption mkMerge mapAttrsToList; - - home-submodule = { name, ... }: { - options = { - proxy = mkOption { - type = types.attrs; - default = { }; - description = "Configuration to be proxied to the home-manager configuration for `home-manager.users.`."; - }; - }; - }; - - cfg = config.snowfallorg; -in -{ - options.snowfallorg = { - home = mkOption { - type = types.attrsOf (types.submodule home-submodule); - default = { }; - description = "Options for configuring home environments."; - }; - }; - - config = mkMerge - (mapAttrsToList - (name: value: { - home-manager.users.${name} = value.proxy; - }) - (cfg.home)); -} diff --git a/modules/home/os/default.nix b/modules/home/os/default.nix deleted file mode 100644 index 2da065f..0000000 --- a/modules/home/os/default.nix +++ /dev/null @@ -1,28 +0,0 @@ -{ lib, osConfig ? { }, ... }: - -let - inherit (lib) types mkOption; - - home-submodule = { name, ... }: { - options = { - proxy = mkOption { - type = types.attrs; - default = { }; - description = "Configuration to be proxied to the home-manager configuration for `home-manager.users.`."; - }; - }; - }; -in -{ - options.snowfallorg = { - home = mkOption { - type = types.attrsOf (types.submodule home-submodule); - default = { }; - description = "Options for configuring home environments."; - }; - }; - - config = { - # snowfallorg.home = osConfig.snowfallorg.home or { }; - }; -} diff --git a/modules/home/user/default.nix b/modules/home/user/default.nix new file mode 100644 index 0000000..559edc5 --- /dev/null +++ b/modules/home/user/default.nix @@ -0,0 +1,23 @@ +{ lib, options, ... }: + +let + inherit (lib) types mkOption mkIf; + + cfg = options.snowfallorg; +in + # (builtins.trace (cfg.user.name or "no name")) +{ + options.snowfallorg = { + user = { + name = mkOption { + type = types.str; + description = "The user's name."; + }; + }; + }; + + # config = mkIf ((cfg.user.name or null) != null) { + # @TODO(jakehamilton): Get user home directory from osConfig if + # it exists. + # }; +} diff --git a/modules/nixos/home/default.nix b/modules/nixos/home/default.nix deleted file mode 100644 index e9cd0a8..0000000 --- a/modules/nixos/home/default.nix +++ /dev/null @@ -1,33 +0,0 @@ -{ lib, config, ... }: - -let - inherit (lib) types mkOption mkMerge mapAttrsToList; - - home-submodule = { name, ... }: { - options = { - proxy = mkOption { - type = types.attrs; - default = { }; - description = "Configuration to be proxied to the home-manager configuration for `home-manager.users.`."; - }; - }; - }; - - cfg = config.snowfallorg; -in -{ - options.snowfallorg = { - home = mkOption { - type = types.attrsOf (types.submodule home-submodule); - default = { }; - description = "Options for configuring home environments."; - }; - }; - - config = mkMerge - (mapAttrsToList - (name: value: { - home-manager.users.${name} = value.proxy; - }) - (cfg.home)); -}