Can't enable home manager in WSL NixOS

I’m trying to setup home manager in NixOS running in WSL. However, it seems my import is not working correctly. I’m not sure what I’m missing. Google and ChatGPT were of no use here.

My /etc/nixos/configuration.nix

{ config, lib, pkgs, ... }:

let
  home-manager = builtins.fetchTarball "https://github.com/nix-community/home-manager/archive/master.tar.gz";
in
{
  imports = [
    # include NixOS-WSL modules
    <nixos-wsl/modules>
    <home-manager/nixos>
  ];

  wsl.enable = true;
  wsl.defaultUser = "nixos";


  # This value determines the NixOS release from which the default
  # settings for stateful data, like file locations and database versions
  # on your system were taken. It's perfectly fine and recommended to leave
  # this value at the release version of the first install of this system.
  # Before changing this value read the documentation for this option
  # (e.g. man configuration.nix or on https://nixos.org/nixos/options.html).
  environment.systemPackages = with pkgs; [
    vim
    oh-my-zsh
    zsh
    zsh-completions
    zsh-powerlevel10k
    zsh-syntax-highlighting
    zsh-history-substring-search
    # other packages
  ];
  system.stateVersion = "24.05"; # Did you read the comment?
  users.users.nixos.isNormalUser = true;
  home-manager.users.nixos = { pkgs, ... }: {
    home.stateVersion = "24.05";
    home.username = "nixos";
    users.defaultUserShell = pkgs.zsh;
    programs.zsh = {
      enable = true;
      oh-my-zsh = {
        enable = true;
        theme = "avit";  # Set the desired theme here
        plugins = [ "git" "vim" "sudo" "z"];
      };
    };
  };

}

The error I get is:

$ sudo nixos-rebuild switch
building Nix...
building the system configuration...
error:
       … while calling the 'head' builtin

         at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/attrsets.nix:1575:11:

         1574|         || pred here (elemAt values 1) (head values) then
         1575|           head values
             |           ^
         1576|         else

       … while evaluating the attribute 'value'

         at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:809:9:

          808|     in warnDeprecation opt //
          809|       { value = builtins.addErrorContext "while evaluating the option `${showOption loc}':" value;
             |         ^
          810|         inherit (res.defsFinal') highestPrio;

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

       error: The option `home-manager.users.nixos.users' does not exist. Definition values:
       - In `/etc/nixos/configuration.nix':
           {
             defaultUserShell = <derivation zsh-5.9>;

What am I missing? I can’t make sense of this error message, it feels like I’m doing the import wrong but I’m not sure.

Try running with --show-trace so that we can see the full stack trace

Here is a fixed configuration

{ config, lib, pkgs, ... }:

let
  home-manager = builtins.fetchTarball "https://github.com/nix-community/home-manager/archive/master.tar.gz";
in
{
  imports = [
    # include NixOS-WSL modules
    <nixos-wsl/modules>
    <home-manager/nixos>
  ];

  wsl.enable = true;
  wsl.defaultUser = "nixos";


  # This value determines the NixOS release from which the default
  # settings for stateful data, like file locations and database versions
  # on your system were taken. It's perfectly fine and recommended to leave
  # this value at the release version of the first install of this system.
  # Before changing this value read the documentation for this option
  # (e.g. man configuration.nix or on https://nixos.org/nixos/options.html).
  environment.systemPackages = with pkgs; [
    vim
    oh-my-zsh
    zsh
    zsh-completions
    zsh-powerlevel10k
    zsh-syntax-highlighting
    zsh-history-substring-search
    # other packages
  ];
  system.stateVersion = "24.05"; # Did you read the comment?
  users.users.nixos.isNormalUser = true;
  users.defaultUserShell = pkgs.zsh;
  programs.zsh.enable = true;
  home-manager.users.nixos = { pkgs, ... }: {
    home.stateVersion = "24.05";
    home.username = "nixos";
    programs.zsh = {
      enable = true;
      oh-my-zsh = {
        enable = true;
        theme = "avit";  # Set the desired theme here
        plugins = [ "git" "vim" "sudo" "z"];
      };
    };
  };

}

I removed users.defaultUserShell from the home-manager configuration and added to the nixos configuration.
In addition, I added programs.zsh.enable = true; to your nixos configuration.

I would also recommend removing the let home-manager = ...; in from the top as it appears that home manager is instead managed through nix channels although this might be wrong.

Ah, I see now that the error told me everything I need to know with “defaultUserShell”. It’s a shame it doesn’t give line numbers.

Thanks so much for taking the time to give me a fixed config, everything is now working!