156: Improve Home Manager support: profiles/suites, modules, extern, flake outputs r=Pacman99 a=Pacman99

A really simple method of implementing #119. 

This relies on a feature that I added in home-manager master for module options of extraSpecialArgs and sharedModules, nix-community/home-manager#1793. I could try and get that backported to 20.09. But I thought I'd get some feedback on these changes first.

Co-authored-by: Pacman99 <pachum99@gmail.com>
This commit is contained in:
bors[bot] 2021-03-23 17:18:43 +00:00 committed by GitHub
commit 74c23ce9c1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 86 additions and 37 deletions

13
extern/default.nix vendored
View file

@ -1,7 +1,4 @@
{ inputs }: with inputs; { inputs }: with inputs;
let
hmModules = { };
in
{ {
modules = [ modules = [
home.nixosModules.home-manager home.nixosModules.home-manager
@ -19,9 +16,15 @@ in
# passed to all nixos modules # passed to all nixos modules
specialArgs = { specialArgs = {
inherit hmModules;
overrideModulesPath = "${override}/nixos/modules"; overrideModulesPath = "${override}/nixos/modules";
hardware = nixos-hardware.nixosModules; hardware = nixos-hardware.nixosModules;
}; };
# added to home-manager
userModules = [
];
# passed to all home-manager modules
userSpecialArgs = {
};
} }

View file

@ -118,16 +118,15 @@
] ]
}, },
"locked": { "locked": {
"lastModified": 1612902587, "lastModified": 1616318638,
"narHash": "sha256-VZ7Z1OMFf5ReObu3CCVBsC2DWloTYDjqW1onUKejVrE=", "narHash": "sha256-E6ABXtzw6bHmrIirB1sJL6S2MEa3sfcvRLzRa92frCo=",
"owner": "nix-community", "owner": "nix-community",
"repo": "home-manager", "repo": "home-manager",
"rev": "209566c752c4428c7692c134731971193f06b37c", "rev": "ddcd476603dfd3388b1dc8234fa9d550156a51f5",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "nix-community", "owner": "nix-community",
"ref": "release-20.09",
"repo": "home-manager", "repo": "home-manager",
"type": "github" "type": "github"
} }

View file

@ -18,7 +18,7 @@
devshell.url = "github:numtide/devshell"; devshell.url = "github:numtide/devshell";
flake-compat.url = "github:BBBSnowball/flake-compat/pr-1"; flake-compat.url = "github:BBBSnowball/flake-compat/pr-1";
flake-compat.flake = false; flake-compat.flake = false;
home.url = "github:nix-community/home-manager/release-20.09"; home.url = "github:nix-community/home-manager";
home.inputs.nixpkgs.follows = "nixos"; home.inputs.nixpkgs.follows = "nixos";
naersk.url = "github:nmattia/naersk"; naersk.url = "github:nmattia/naersk";
naersk.inputs.nixpkgs.follows = "override"; naersk.inputs.nixpkgs.follows = "override";
@ -47,10 +47,16 @@
}); });
}); });
homeConfigurations = os.mkHomeConfigurations;
nixosModules = nixosModules =
let moduleList = import ./modules/module-list.nix; let moduleList = import ./modules/module-list.nix;
in lib.pathsToImportedAttrs moduleList; in lib.pathsToImportedAttrs moduleList;
homeModules =
let moduleList = import ./users/modules/module-list.nix;
in lib.pathsToImportedAttrs moduleList;
overlay = import ./pkgs; overlay = import ./pkgs;
overlays = lib.pathsToImportedAttrs (lib.pathsIn ./overlays); overlays = lib.pathsToImportedAttrs (lib.pathsIn ./overlays);
@ -82,9 +88,6 @@
devShell = import ./shell { devShell = import ./shell {
inherit self system; inherit self system;
}; };
legacyPackages.hmActivationPackages =
os.mkHomeActivation;
} }
); );
in in

View file

@ -35,8 +35,13 @@ let
}; };
global = { config, ... }: { global = { config, ... }: {
home-manager.useGlobalPkgs = true; home-manager = {
home-manager.useUserPackages = true; useGlobalPkgs = true;
useUserPackages = true;
extraSpecialArgs = extern.userSpecialArgs // { suites = suites.user; };
sharedModules = extern.userModules ++ (builtins.attrValues self.homeModules);
};
hardware.enableRedistributableFirmware = lib.mkDefault true; hardware.enableRedistributableFirmware = lib.mkDefault true;
@ -67,7 +72,7 @@ let
flakeModules = { imports = builtins.attrValues self.nixosModules ++ extern.modules; }; flakeModules = { imports = builtins.attrValues self.nixosModules ++ extern.modules; };
}; };
specialArgs = extern.specialArgs // { inherit suites; }; specialArgs = extern.specialArgs // { suites = suites.system; };
mkHostConfig = hostName: mkHostConfig = hostName:
let let

View file

@ -19,7 +19,7 @@
devosSystem = dev.callLibs ./devosSystem.nix; devosSystem = dev.callLibs ./devosSystem.nix;
mkHomeActivation = dev.callLibs ./mkHomeActivation.nix; mkHomeConfigurations = dev.callLibs ./mkHomeConfigurations.nix;
mkPackages = dev.callLibs ./mkPackages.nix; mkPackages = dev.callLibs ./mkPackages.nix;
} }

View file

@ -73,10 +73,29 @@ lib.nixosSystem (args // {
}) })
]; ];
})).config; })).config;
hmConfig = (lib.nixosSystem
(args // {
modules = moduleList ++ [
({ config, ... }: {
home-manager.useUserPackages = lib.mkForce false;
home-manager.sharedModules = [
{
home.packages = config.environment.systemPackages;
home.sessionVariables = {
inherit (config.environment.sessionVariables) NIX_PATH;
};
xdg.configFile."nix/registry.json".text =
config.environment.etc."nix/registry.json".text;
}
];
})
];
})).config;
in in
moduleList ++ [{ moduleList ++ [{
system.build = { system.build = {
iso = isoConfig.system.build.isoImage; iso = isoConfig.system.build.isoImage;
homes = hmConfig.home-manager.users;
}; };
}]; }];
}) })

View file

@ -1,12 +0,0 @@
{ lib, self, ... }:
let hmConfigs =
lib.mapAttrs
(_: config: config.config.home-manager.users)
self.nixosConfigurations;
in
lib.mapAttrs
(_: x: lib.mapAttrs
(_: cfg: cfg.home.activationPackage)
x)
hmConfigs

View file

@ -0,0 +1,12 @@
{ lib, self, ... }:
with lib;
let
mkHomes = host: config:
mapAttrs' (user: v: nameValuePair "${user}@${host}" v)
config.config.system.build.homes;
hmConfigs = mapAttrs mkHomes self.nixosConfigurations;
in
foldl recursiveUpdate {} (attrValues hmConfigs)

View file

@ -81,7 +81,7 @@ case "$1" in
;; ;;
"home") "home")
ref="$DEVSHELL_ROOT/#hmActivationPackages.$2.$3" ref="$DEVSHELL_ROOT/#homeConfigurations.$3@$2.home.activationPackage"
if [[ "$4" == "switch" ]]; then if [[ "$4" == "switch" ]]; then
nix build "$ref" && result/activate && nix build "$ref" && result/activate &&

View file

@ -3,6 +3,7 @@ let
inherit (lib) dev; inherit (lib) dev;
profiles = dev.os.mkProfileAttrs (toString ../profiles); profiles = dev.os.mkProfileAttrs (toString ../profiles);
userProfiles = dev.os.mkProfileAttrs (toString ../users/profiles);
users = dev.os.mkProfileAttrs (toString ../users); users = dev.os.mkProfileAttrs (toString ../users);
allProfiles = allProfiles =
@ -17,7 +18,18 @@ let
suites = with profiles; rec { suites = with profiles; rec {
base = [ users.nixos users.root ]; base = [ users.nixos users.root ];
}; };
# available as 'suites' within the home-manager configuration
userSuites = with userProfiles; rec {
base = [ direnv git ];
};
in in
lib.mapAttrs (_: v: dev.os.profileMap v) suites // { {
inherit allProfiles allUsers; system = lib.mapAttrs (_: v: dev.os.profileMap v) suites // {
inherit allProfiles allUsers;
};
user = lib.mapAttrs (_: v: dev.os.profileMap v) userSuites // {
allProfiles = userProfiles;
};
} }

View file

@ -22,10 +22,17 @@ your users. For a fully fleshed out example, check out the developers personal
``` ```
## Home Manager
Home Manager support follows the same principles as regular nixos configurations.
All modules defined in [user modules](./modules/module-list.nix) will be imported to
Home Manager. All profiles are availabe in [suites](../suites/default.nix) as userProfiles.
The `userSuites` output will be available in your Home Manager Configuration as
the special argument, `suites`.
## External Usage ## External Usage
You can easily use the defined home-manager configurations outside of NixOS You can easily use the defined home-manager configurations outside of NixOS
using the `hmActivations` meta-package defined in the flakes `legacyPackages` using the `homeConfigurations` flake output. The [flk](../doc/flk) helper
output. The [flk](../doc/flk) helper script makes this even easier. script makes this even easier.
This is great for keeping your environment consistent across Unix systems, This is great for keeping your environment consistent across Unix systems,
including OSX. including OSX.
@ -42,7 +49,7 @@ flk home NixOS nixos switch
### Manually from outside the project: ### Manually from outside the project:
```sh ```sh
# build # build
nix build "github:divnix/devos#hmActivationPackages.NixOS.nixos" nix build "github:divnix/devos#homeConfigurations.nixos@NixOS.home.activationPackage"
# activate # activate
./result/activate && unlink result ./result/activate && unlink result

View file

@ -0,0 +1 @@
[]

View file

@ -1,7 +1,7 @@
{ ... }: { ... }:
{ {
home-manager.users.nixos = { home-manager.users.nixos = { suites, ... }: {
imports = [ ../profiles/git ../profiles/direnv ]; imports = suites.base;
}; };
users.users.nixos = { users.users.nixos = {