Help with home-manager flake config

Hi everyone,

I am trying to switch my home-manager configuration to flakes following the example configuration from @mjlbach: https://github.com/mjlbach/nix-dotfiles/blob/329d3c847e8895f21698db0253d7a0cadb6ce0e9/home-manager/flake.nix

{
  description = "NixOS configuration";

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

  outputs = inputs@{ self, ... }:
  let
    overlays = [
        (import /home/bogdb/.config/nixpkgs/overlays/default.nix)
        inputs.neovim-nightly.overlay
    ];
  in
  inputs.flake-utils.lib.eachDefaultSystem (system:
  {
    legacyPackages = inputs.nixpkgs.legacyPackages.${system};
  }
  ) //
  {
    homeConfigurations = {
      bogdb = inputs.home-manager.lib.homeManagerConfiguration {
        stateVersion = "21.05";
        system = "x86_64-linux";
        homeDirectory = "/home/bogdb";
        username = "bogdb";

        configuration = { pkgs, ... }:
        {
          nixpkgs.config = import "/home/bogdb/.config/nixpkgs/config.nix";
          nixpkgs.overlays = overlays;

          programs.home-manager.enable = true;

          home.packages = with pkgs; [
            # utilities
            alacritty
            bat
            chessx
            datamash
            exa
            fd
            flac
            flacon
            gh
            gitui
            ...
          ];

          home.sessionVariables = {
            EDITOR = "nvim";
            TCLLIBPATH = "~/.local/share/tk-themes";
          };

          programs.bash = {
            enable = true;
            shellAliases = {
              nv = "nvim-qt -- -u ~/.config/nvim/init.vim";
              kd = "kitty --detach";
            };
          };
          programs.vscode = {
            enable = true;
            package = pkgs.vscode;
            extensions = with pkgs.vscode-extensions; [
              xaver.clang-format
              ms-vscode.cpptools
            ];
          };
          programs.neovim = {
            enable = true;
            withPython3 = true;
            withNodeJs = true;
            plugins = with pkgs.vimPlugins; [
              { plugin = awesome-vim-colorschemes; }
              { plugin = nvim-lspconfig; }
              { plugin = nvim-treesitter; }
              { plugin = completion-nvim; }
              { plugin = lualine-nvim; }
              { plugin = fzf-vim; }
              { plugin = lazygit-nvim; }
              { plugin = nerdtree; }
              { plugin = nvim-base16; }
              { plugin = vim-nix; }
              { plugin = vim-pandoc-syntax; }
              { plugin = vim-pandoc; }
              { plugin = vimtex; }
            ];
            extraConfig = ''
              if !exists("homemanagerbug")
              let homemanagerbug = "yes"
              luafile /home/bogdb/.config/nvim/init.lua
              endif
            '';
          }; # neovim

        }; # configuration
      };
    }; # homeConfigurations
    bogdb = self.homeConfigurations.bogdb.activationPackage;
    defaultPackage.x86_64-linux = self.bogdb;
  }; #outputs
}

However I ran into some problems. If I try to rebuild my home using nixos-rebuild I get:

$ nixos-rebuild --flake .config/nixpkgs/  switch
building the system configuration...
error: flake 'path:/home/bogdb/.config/nixpkgs?narHash=sha256-Kh%2fjUo8skLwdFfFl46YzZ5mCWZnt3RQsvSDNOZHqsio=' does not provide attribute 'packages.x86_64-linux.nixosConfigurations."jaghut".config.system.build.toplevel', 'legacyPackages.x86_64-linux.nixosConfigurations."jaghut".config.system.build.toplevel' or 'nixosConfigurations."jaghut".config.system.build.toplevel'

If I try calling home-manager switch:

$ home-manager switch
error: access to path '/home/bogdb/.config/nixpkgs/config.nix' is forbidden in restricted mode
(use '--show-trace' to show detailed location information)

The only thing that works is $ nix build .config/nixpkgs/.# --impure followed by manual activation of the result.

I’d be very grateful if anyone could point out my mistakes and help me understand this. Thanks.

Your flake doesn’t contain a NixOS configuration, so nixos-rebuild has nothing to build.

Instead try home-manager switch --flake .

The home-manager github README specifies:

When using flakes, switch to new configurations as you do for the whole system (e. g. nixos-rebuild switch --flake <path> ) instead of using the home-manager command line tool.

Which is why I tried the first option. It looks like I am indeed missing the nixosConfiguration, which suggests that my home manager flake is not integrated properly with the system conf. I thought I can just flake the home manager without doing a system wide flake conversion.

home-manager switch does not need to be passed --flake because it will switch it on automatically if a flake.nix is detected. But the problem is that home-manager does not take --impure as an option.

That’s a switch we forgot then :slight_smile: You can raise an issue upstream.

Though arguably this is just an issue with your configuration here, no need to make anything impure. Where you do this:

Simply make the import path relative, assuming you’re in ~/.config/nixpkgs, or use a flake input instead of a raw file path (note you can set the flake attribute on an import to false if you want to use a non-flake as input).

@TLATER Thanks, your advice worked, making the import paths relatives allowed me to call home-manager switch. Everything seems to be working fine now!