Add an option to home manager as a nixos module using flake

Hello, I got some problem while trying out ags. I use home manager as an nixos configuration module. Specifically, my home.nix is located in /etc/nixos with the following content:

#file: /etc/nixos/home.nix
{ config, pkgs, ... }:
let
  home-manager = builtins.fetchTarball "https://github.com/nix-community/home-manager/archive/release-23.11.tar.gz";
in
{
  imports = [
    (import "${home-manager}/nixos")
  ];

  home-manager.users.${username} = { inputs, config, pkgs, lib, ...}: {
#abcxyz
  };
}

and so, it obviously doesn’t work with this flake:

#file: /etc/nixos/flake.nix
{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-23.11-darwin";
    home-manager = {
      url = "github:nix-community/home-manager";
      inputs.nixpkgs.follows = "nixpkgs";
    };

    # add ags
    ags.url = "github:Aylur/ags";
  };

  outputs = { home-manager, nixpkgs, ... }@inputs:
  let
    system = "x86_64-linux";
  in
  {
    homeConfigurations."${username}" = home-manager.lib.homeManagerConfiguration {
      pkgs = import nixpkgs { inherit system; };

      # pass inputs as specialArgs
      extraSpecialArgs = { inherit inputs; };

      # import your home.nix
      modules = [ ./home.nix ];
    };
  };
}

and the error:

warning: creating lock file '/etc/nixos/flake.lock'
error: flake 'path:/etc/nixos' does not provide attribute 'packages.x86_64-linux.nixosConfigurations."Windows11".config.system.build.nixos-rebuild', 'legacyPackages.x86_64-linux.nixosConfigurations."Windows11".config.system.build.nixos-rebuild' or 'nixosConfigurations."Windows11".config.system.build.nixos-rebuild'

How would you suggest i change the content of flake so that it work with my current config? I don’t know anything about flake or how it works at all :frowning:

I love home manager and I dont want it to be seperate so that i can do rebuild easier and don’t have to worry about having to do extra steps

Thank you in advance

{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
    home-manager = {
      url = "github:nix-community/home-manager";
      inputs.nixpkgs.follows = "nixpkgs";
    };

    # add ags
    ags.url = "github:Aylur/ags";
  };

  outputs = { home-manager, nixpkgs, ... }@inputs:
  let
    username = "kahlenden";
    hostname = "Windows11";
    system = "x86_64-linux";
    pkgs = import nixpkgs {
      inherit system;
      config.allowUnfree = true;
    };
  in
  {
    nixosConfigurations.${hostname} = nixpkgs.lib.nixosSystem {
      specialArgs = { inherit inputs username system; };
      modules = [ ./configuration.nix
                  home-manager.nixosModules.home-manager {
                    home-manager.useGlobalPkgs = true;
                    home-manager.useUserPackages = true;
                    home-manager.users.${username} = import ./home.nix;
                    home-manager.extraSpecialArgs = {inherit inputs username; };
                  }
      ];
    };
  };
}

This is what i came up with, still error tho, no idea what went wrong

error:
       … while calling the 'seq' builtin

         at /nix/store/dw3dzpv6yqrw2gikw9qgnda0sy8xvlwi-source/lib/modules.nix:322:18:

          321|         options = checked options;
          322|         config = checked (removeAttrs config [ "_module" ]);
             |                  ^
          323|         _module = checked (config._module);

       … while evaluating a branch condition

         at /nix/store/dw3dzpv6yqrw2gikw9qgnda0sy8xvlwi-source/lib/modules.nix:261:9:

          260|       checkUnmatched =
          261|         if config._module.check && config._module.freeformType == null && merged.unmatchedDefns != [] then
             |         ^
          262|           let

       (stack trace truncated; use '--show-trace' to show the full trace)

       error: in pure evaluation mode, 'fetchTarball' requires a 'sha256' argument

Flakes run in pure evaluation mode. That means fetchTarball as you do in your home.nix is not allowed:

It is not allowed because Nix has no way to knowing whether the file will be the same on future downloads (it might actually change) so your flake would not be guaranteed to be reproducible.

Not sure why the error trace does not point to the fetchTarball location (maybe it requires --show-trace)


But you do not really need to fetch the home-manager manually, when you already have it present through flakes. You can pass the home-manager from flake.nix through extraSpecialArgs and then use it in home.nix.


This is because you have no nixosConfigurations. nixos-rebuild requires NixOS configuration. If you want to have home-manager activated as part of NixOS system activation, you will need to add home-manager NixOS module to the system configuration and then configure it as described in Home manager, flake and configuration. How to rebuild the whole environment with just nixos-rebuild switch? - #3 by jtojnar.


Additionally, you are importing a home-manager NixOS module into home.nix (a home-manager module) which is not allowed – home-manager would have no idea what to do with the NixOS options defined by the home-manager NixOS module.

home.nix is a home-manager module because you pass it to modules argument of homeManagerConfiguration.

Thank you, as always, but i have a new question:

homeConfigurations."${username}" = home-manager.lib.homeManagerConfiguration {
      inherit pkgs;
      extraSpecialArgs = { inherit inputs; };
      modules = [ 
        ./home.nix
        hyprland.homeManagerModules.default
        {wayland.windowManager.hyprland.enable = true;}
      ];

Specifically, how do i import the hyprland home manager module to this:

nixosConfigurations.${hostname} = nixpkgs.lib.nixosSystem {
      specialArgs = { inherit inputs; };
      modules = [ 
        ./configuration.nix
        home-manager.nixosModules.home-manager {
          home-manager.useGlobalPkgs = true;
          home-manager.useUserPackages = true;
          home-manager.users.${username} = import ./home.nix;
          home-manager.extraSpecialArgs = {inherit inputs username; };
        }
      ];
    };

Since you are passing inputs to home-manager.extraSpecialArgs, you can move the hyperland hm module declaration into home.nix:

{ config, pkgs, inputs, ... }:

{
  imports = [
    inputs.hyprland.homeManagerModules.default
  ];

  wayland.windowManager.hyprland.enable = true;
}