Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Home Manager support: profiles/suites, modules, extern, flake outputs #156

Merged
merged 5 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions extern/default.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{ inputs }: with inputs;
let
hmModules = { };
in
{
modules = [
home.nixosModules.home-manager
Expand All @@ -19,9 +16,15 @@ in

# passed to all nixos modules
specialArgs = {
inherit hmModules;

overrideModulesPath = "${override}/nixos/modules";
hardware = nixos-hardware.nixosModules;
};

# added to home-manager
userModules = [
];

# passed to all home-manager modules
userSpecialArgs = {
};
}
7 changes: 3 additions & 4 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
devshell.url = "github:numtide/devshell";
flake-compat.url = "github:BBBSnowball/flake-compat/pr-1";
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";
naersk.url = "github:nmattia/naersk";
naersk.inputs.nixpkgs.follows = "override";
Expand Down Expand Up @@ -47,10 +47,16 @@
});
});

homeConfigurations = os.mkHomeConfigurations;

nixosModules =
let moduleList = import ./modules/module-list.nix;
in lib.pathsToImportedAttrs moduleList;

homeModules =
let moduleList = import ./users/modules/module-list.nix;
in lib.pathsToImportedAttrs moduleList;

overlay = import ./pkgs;
overlays = lib.pathsToImportedAttrs (lib.pathsIn ./overlays);

Expand Down Expand Up @@ -82,9 +88,6 @@
devShell = import ./shell {
inherit self system;
};

legacyPackages.hmActivationPackages =
os.mkHomeActivation;
}
);
in
Expand Down
11 changes: 8 additions & 3 deletions hosts/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ let
};

global = { config, ... }: {
home-manager.useGlobalPkgs = true;
home-manager.useUserPackages = true;
home-manager = {
useGlobalPkgs = true;
useUserPackages = true;

extraSpecialArgs = extern.userSpecialArgs // { suites = suites.user; };
nrdxp marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may need to add inherit (config.nixpkgs) pkgs as home-manager doesn't seem to have a pkgs argument. This may be a bug upstream however, as I believe home-manager is supposed to pass in packages. In practice though, at least with this implementation, I had to add a pkgs arg to get doom-emacs to work, for example.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure thats the problem? Because home-manager already does that when useGlobalPkgs is true.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

" using the system configuration's pkgs
argument in Home Manager. This disables the Home Manager
options nixpkgs.*"
^ from hm nixos module

Copy link
Collaborator

@nrdxp nrdxp Mar 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran into these issues while trying to merge this into community. I just pushed the merge so you should now be able to reproduce by simply checking out the latest community branch and running flk home NixOS nixos

Copy link
Collaborator

@nrdxp nrdxp Mar 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps shared modules don't get passed a pkgs arg, since that is how doom-emacs is entering the configuration. Not sure if this intentional by upstream or a bug. I'll search hm issue tracker to see if I can find some additional context.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be true, I tried adding pkgs to the hm section in users/nixos and used it to add vim to home.packages and that works fine. So your theory does make sense.

Copy link
Member Author

@Pacman99 Pacman99 Mar 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In home manager, the ways pkgs is set is using _module.args.pkgs in home-manager/modules/modules.nix. And I wonder if doesn't get passed to modules that were imported before that. But that doesn't make sense to me, since the module system should be recursive.

Copy link
Collaborator

@nrdxp nrdxp Mar 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe I've isolated the problem. Gonna fix and test locally then will link to an new home-manager PR if I'm correct.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, I've offered a minimally invasive solution in nix-community/home-manager#1879

sharedModules = extern.userModules ++ (builtins.attrValues self.homeModules);
};

hardware.enableRedistributableFirmware = lib.mkDefault true;

Expand Down Expand Up @@ -67,7 +72,7 @@ let
flakeModules = { imports = builtins.attrValues self.nixosModules ++ extern.modules; };
};

specialArgs = extern.specialArgs // { inherit suites; };
specialArgs = extern.specialArgs // { suites = suites.system; };

mkHostConfig = hostName:
let
Expand Down
2 changes: 1 addition & 1 deletion lib/devos/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

devosSystem = dev.callLibs ./devosSystem.nix;

mkHomeActivation = dev.callLibs ./mkHomeActivation.nix;
mkHomeConfigurations = dev.callLibs ./mkHomeConfigurations.nix;

mkPackages = dev.callLibs ./mkPackages.nix;
}
Expand Down
19 changes: 19 additions & 0 deletions lib/devos/devosSystem.nix
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,29 @@ lib.nixosSystem (args // {
})
];
})).config;
hmConfig = (lib.nixosSystem
(args // {
modules = moduleList ++ [
({ config, ... }: {
home-manager.useUserPackages = lib.mkForce false;
home-manager.sharedModules = [
{
home.packages = config.environment.systemPackages;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hindsight review:

As it turns out, this may be a bit too aggressive. We may have to have a follow up PR to remove it, as it includes a lot of unrelated system packages and, worse, causes package conflicts in the community branch.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah thats fine we can remove that line. It worked for me and I didn't think it would cause any issues.

home.sessionVariables = {
inherit (config.environment.sessionVariables) NIX_PATH;
};
xdg.configFile."nix/registry.json".text =
config.environment.etc."nix/registry.json".text;
}
];
})
];
})).config;
in
moduleList ++ [{
system.build = {
iso = isoConfig.system.build.isoImage;
homes = hmConfig.home-manager.users;
};
}];
})
12 changes: 0 additions & 12 deletions lib/devos/mkHomeActivation.nix

This file was deleted.

12 changes: 12 additions & 0 deletions lib/devos/mkHomeConfigurations.nix
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion shell/flk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ case "$1" in
;;

"home")
ref="$DEVSHELL_ROOT/#hmActivationPackages.$2.$3"
ref="$DEVSHELL_ROOT/#homeConfigurations.$3@$2.home.activationPackage"

if [[ "$4" == "switch" ]]; then
nix build "$ref" && result/activate &&
Expand Down
16 changes: 14 additions & 2 deletions suites/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ let
inherit (lib) dev;

profiles = dev.os.mkProfileAttrs (toString ../profiles);
userProfiles = dev.os.mkProfileAttrs (toString ../users/profiles);
users = dev.os.mkProfileAttrs (toString ../users);

allProfiles =
Expand All @@ -17,7 +18,18 @@ let
suites = with profiles; rec {
base = [ users.nixos users.root ];
};

# available as 'suites' within the home-manager configuration
userSuites = with userProfiles; rec {
base = [ direnv git ];
};

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;
};
}
13 changes: 10 additions & 3 deletions users/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
You can easily use the defined home-manager configurations outside of NixOS
using the `hmActivations` meta-package defined in the flakes `legacyPackages`
output. The [flk](../doc/flk) helper script makes this even easier.
using the `homeConfigurations` flake output. The [flk](../doc/flk) helper
script makes this even easier.

This is great for keeping your environment consistent across Unix systems,
including OSX.
Expand All @@ -42,7 +49,7 @@ flk home NixOS nixos switch
### Manually from outside the project:
```sh
# build
nix build "github:divnix/devos#hmActivationPackages.NixOS.nixos"
nix build "github:divnix/devos#homeConfigurations.nixos@NixOS.home.activationPackage"

# activate
./result/activate && unlink result
Expand Down
1 change: 1 addition & 0 deletions users/modules/module-list.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
4 changes: 2 additions & 2 deletions users/nixos/default.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{ ... }:
{
home-manager.users.nixos = {
imports = [ ../profiles/git ../profiles/direnv ];
home-manager.users.nixos = { suites, ... }: {
imports = suites.base;
};

users.users.nixos = {
Expand Down