Problem setting Git options with home manager

New to NixOS, I’m getting started on setting up a home server machine, trying to get to the “base camp” level where I have a basic working config linked to a Git repo (GitLab) so i have a means to track changes and rollback the inevitable errors I will make.

I decided to use just basic nix plus home-manager initially, without involving flakes, because Nix+home-manager+flakes is more than my poor brain can absorb in one go. I also went for the “module based” approach to home manager, so all the config is currently in configuration.nix.

The issue: The configuration I have so far is NOT correctly setting the key Git environment variables (userName, userEmail, defaultBranch) so I can’t get the link to GitLab working.

configuration.nix (relevant parts, redacted)

 config, pkgs, ... }:

{
  imports =
    [
      ./hardware-configuration.nix
      <home-manager/nixos>
    ];

  ...

  # User accounts
  users.users.me = {
    isNormalUser = true;
    description = "Me";
    extraGroups = [ "networkmanager" "wheel" ];
    shell = pkgs.zsh;
  };

  # Home-manager config
  home-manager.useUserPackages = true;  # Packages install to /etc/profiles
  home-manager.useGlobalPkgs = true;    # Use global package definitions

  # Configure user accounts with home-manager
  home-manager.users.me = { pkgs, ... }: {
    home.stateVersion = "24.05";  # Required, static
    home.packages = with pkgs; [
      vscode-fhs	# Allows extensions to be added directly via the code UI
    ];

    programs.git = {
      enable = true;
      userName = "GitLab Username";
      userEmail = "GitLab Email";
      extraConfig = {
        init.defaultBranch = "main";
      };
    };
  };

  # Allow unfree packages
  nixpkgs.config.allowUnfree = true;

  # List packages installed in system profile. To search, run:
  # $ nix search wget
  environment.systemPackages = with pkgs; [
    git
    wget
    curl
    home-manager
    zsh
    keepassxc
    firefox
    thunderbird
  ];

  # Enable the OpenSSH daemon.
  services.openssh.enable = true;

  # State version
  system.stateVersion = "24.05"; # Required, static
}

This (in original unredacted form) builds ok, but if I open a new shell the git environment settings have not taken effect-

$> git config --list
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=https://gitlab.com/__myrepo__
remote.origin.fetch=+refs/heads/:refs/remotes/origin/

the “userName” and “userEmail” etc. global settings are not present. I assume I’m missing something basic here. Any hints gratefully received.

My config looks like this:

extraConfig = {
      user = {
        name = "my name";
        email = "my@email.com";
      };

Thanks, I tried adapting my setup to the “extraConfig” format you showed like this-

  home-manager.users.me = { pkgs, ... }: {
    home.packages = with pkgs; [
      vscode-fhs	# Allows extensions to be added directly via the code UI
    ];

    programs.git = {
      extraConfig = {
        user.name = "GitLab Username";
        user.email = "GitLab Email";
        init.defaultBranch = "main";
      };
    };

    home.stateVersion = "24.05";
  };

but that didn’t work. Can I ask if your setup is using home-manager, or just “raw” configuration.nix?

The examples shown in this video - https://youtu.be/20BN4gqHwaQ?si=AKW9dQtC43j77rDj were the basis for my initial try (using home-manager), and this syntax also seems to be consistent with the home manager documentation for these options, e.g. - Appendix A. Home Manager Configuration Options.

So, i’m left scratching my head on this. Maybe the home-manager module options have changed, leaving the video and the documentation out of date, but if so (and given there are no error messages) i’m not clear how i’m supposed to debug this.

Is the generated config correct in ~/.config/git/config ?

No such folder-

me@Hilly:~/.config/ > ls -l ~/.config/git
ls: cannot access '/home/me/.config/git': No such file or directory

home-manager is doing something because the home.pkgs clause does succeed in making vscode available, but the programs.git section seems to be ineffective.

Well, in the second snippet, git is not enabled:

    programs.git = {
+     enable = true;
      extraConfig = {
        user.name = "GitLab Username";
        user.email = "GitLab Email";
        init.defaultBranch = "main";
      };
    };

Maybe that’s why it doesn’t create it?

1 Like

In the current configuration on my machine git is set up as a system package within environment.systemPackages and at the user profile level with

    programs.git = {
      enable = true;
      ...
      };

as shown in the first example.

I originally thought this was redundant. However I have since found that having a package in environment.systemPackages is not always the same thing as having that package “enabled”. Thunderbird for example works fine just by being declared in systemPackages, but zsh generates warning unless it is also “enabled”. Hopefully at some point this will make more sense.

Yes I missed the enable = true; line out in the second example, while experimenting with @firecat53 's suggestion. I reverted back to the original setup and it does generate a config file, which looks like-

$> more ~/.config/git/config

[init]
        defaultBranch = "main"

[user]
        email = "GitLab Username"
        name = "GitLab Email"

which looks plausible. More importantly git config now shows the results of the nix-side configuration!

I’m very confused as to why it’s behaving differently, I THOUGHT I had reverted back to exactly the same syntax I had originally, and I did start a new terminal+shell for each test to pick up environment changes. This is exactly why i’m trying to get Git set up of course!

So, it seems i’m making progress, even if I don’t understand quite how.

Thanks to everyone who replied.

1 Like