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

Accessing flakes from inside home-manager modules #1698

Closed
andyrichardson opened this issue Jan 4, 2021 · 9 comments · Fixed by #1793
Closed

Accessing flakes from inside home-manager modules #1698

andyrichardson opened this issue Jan 4, 2021 · 9 comments · Fixed by #1793

Comments

@andyrichardson
Copy link

Issue description

Hi there! First off, thanks for the awesome library!

I'm currently trying to use flakes with home manager - however, I'm struggling to access a flake (output argument) in a home-manager module.

Example

Here's an example nix config

flake.nix

{
  description = "NixOS configuration";
  inputs = {
    # Some flake I want to access in a home-manager module
    desiredflake = {
      url = "github:zsh-users/antigen/v2.2.3";
      flake = false;
    };
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    darwin.url = "github:lnl7/nix-darwin/master";
    darwin.inputs.nixpkgs.follows = "nixpkgs";
    home-manager.url = "github:nix-community/home-manager";
    flake-compat = {
      url = "github:edolstra/flake-compat";
      flake = false;
    };
  };
  

  outputs = { desiredflake, home-manager, nixpkgs, darwin, ... }: {
    defaultPackage.x86_64-darwin = (darwin.lib.darwinSystem {
      inputs = { inherit home-manager nixpkgs darwin desiredflake; };
      modules = [ 
        home-manager.darwinModules.home-manager
        {
          home-manager.useUserPackages = true;
          home-manager.users.someuser = import ./home.nix;
        }
      ];
    }).system;
  };
}

home.nix

{ pkgs, ... }:

{
  programs.zsh = {
    enable = true;
    plugins = [
      {
        name = "zsh-nix-shell";
        file = "antigen.zsh";
        src = desiredflake; # How does this flake output get to this module?
      }
    ];
  };
}

Notes

  • It looks like home-manager doesn't forward output arguments of a flake to modules
  • I'm not sure if there is a way to access a flake without having it passed explicitly (e.g. builtins.getFlakeOutput desiredflake)

Meta

Maintainer CC

Technical details

@terlar
Copy link
Contributor

terlar commented Jan 4, 2021

One way to do it is to inject specialArgs into the home-manager default options, that is what I am using and have seen others use, not sure if there is a better way now:

{
  description = "NixOS configuration";
  inputs = {
    # Some flake I want to access in a home-manager module
    desiredflake = {
      url = "github:zsh-users/antigen/v2.2.3";
      flake = false;
    };
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    darwin.url = "github:lnl7/nix-darwin/master";
    darwin.inputs.nixpkgs.follows = "nixpkgs";
    home-manager.url = "github:nix-community/home-manager";
    flake-compat = {
      url = "github:edolstra/flake-compat";
      flake = false;
    };
  };
  

  outputs = { desiredflake, home-manager, nixpkgs, darwin, ... }: {
    defaultPackage.x86_64-darwin = (darwin.lib.darwinSystem {
      inputs = { inherit home-manager nixpkgs darwin desiredflake; };
      modules = [ 
        home-manager.darwinModules.home-manager
        {
          home-manager.useUserPackages = true;
          home-manager.users.someuser = import ./home.nix;
        }
        ({ config, lib, ... }: { 
           options.home-manager.users = lib.mkOption {
             type = with lib.types; attrsOf (submoduleWith {
               specialArgs = { super = config; inherit desiredflake; };
             });
           };
        })
      ];
    }).system;
  };
}

@andyrichardson
Copy link
Author

andyrichardson commented Jan 4, 2021

Thanks @terlar that's really useful!

I'm not sure which is the better option - I managed to get something to work by currying imports like this

home.nix

flakes: { pkgs, ... }:

{
  programs.zsh = {
    enable = true;
    plugins = [
      {
        name = "zsh-nix-shell";
        file = "antigen.zsh";
        src = flakes.desiredflake;
      }
    ];
  };
}
home-manager.users.someuser = import ./home.nix { desiredflake = desiredflake; };

@berbiche
Copy link
Member

berbiche commented Jan 8, 2021

#1699

The changes done in this PR would need to be applied to the NixOS module and the Darwin module.

@berbiche
Copy link
Member

@andyrichardson is this still an issue with #1699 merged?

@thiagokokada
Copy link
Contributor

@berbiche extraSpecialArgs works for standalone HM but not for home-manager.nixosModules.home-manager or home-manager.darwinModules.home-manager.

So I would say this is still an issue, and if someone have some idea on how to fix it would be great (I am still learning how the whole Flake works).

@andyrichardson
Copy link
Author

I've resorted to currying so cannot comment on whether the newest changes have worked.

I've pulled the latest flake but I'm a bit of a nix noob so I wasn't able to work out where to add extraSpecialArgs

@cideM
Copy link

cideM commented May 5, 2021

For the next person who comes across this issue, extraSpecialArgs worked perfectly for me:

{
  outputs = { self, neovim-nightly-overlay, unstable, home-manager, hwConfig, nixpkgs, scripts }:
    {
      nixosConfigurations.nixos =
        let
          system = "x86_64-linux";

          specialArgs = {
            inherit hwConfig neovim-nightly-overlay scripts;
          };

          modules = [
            ./hosts/nixos/configuration.nix
            home-manager.nixosModules.home-manager
            {
              home-manager.useGlobalPkgs = true;
              home-manager.useUserPackages = true;
              home-manager.users.foo = import ./hosts/nixos/home.nix;
              home-manager.extraSpecialArgs = specialArgs;
            }
          ];
        in
        unstable.lib.nixosSystem { inherit system modules specialArgs; };
    };
}

// home.nix can now use "scripts"
{ scripts, ...}: {}

@nixos-discourse
Copy link

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/how-to-export-flake-nix-as-a-package/16227/4

@nixos-discourse
Copy link

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/nixos-flakes-with-home-manager/18476/1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants