Is it possible to change the default git user config for a `devShell`?

Thanks! I ended up with the following:

{ pkgs ? import <nixpkgs> {} }:

pkgs.mkShell {
  buildInputs = [
    # ...
  ];

  GIT_CONFIG_GLOBAL =
    pkgs.writeText
      "git.conf"
      ''
      # https://git-scm.com/docs/git-config#_configuration_file
      # or copy-paste from where home-manager keeps it:
      # ~/.config/git/config
      ''
  ;

  shellHook = ''
    # stuff
  ''
}
NOTE TO FUTURE SELF

If a path is provided to GIT_CONFIG_GLOBAL, then it will be copied to the Nix store (no writeText needed)

For example:

GIT_CONFIG_GLOBAL = ./config_files/git.conf

writeText (and co.) becomes a derivation (i.e., a .drv file in the Nix store) so how can it be assigned to a shell variable as if it was the end result?

nix-repl> pkgs.writeText "lofa" ''
          falho
          vert
          lofa
          ''
«derivation /nix/store/572zrrh4jg2qimw8xfcjjnf75jkv0syp-lofa.drv»

I remember something about this in one of the manuals (that “intermediate” derivation will get instantiated or something), but can’t remember in which (and where)…

Why does GIT_CONFIG_GLOBAL work outside of shellHook?

I don’t think this is documented (see thread), but

  • variables in mkShell but outside shellHook will become ENVIRONMENT VARIABLES
  • variables inside shellHook become SHELL VARIABLES (unless explicitly exported)

So the example above is equivalent to

{ pkgs ? import <nixpkgs> {} }:

pkgs.mkShell {
  buildInputs = [
    # ...
  ];

  shellHook =
    let
      gitConf =
        pkgs.writeText
          "git.conf"
          ''
          # https://git-scm.com/docs/git-config#_configuration_file
          # or copy-paste from where home-manager keeps it:
          # ~/.config/git/config
          ''
      ;
    in
      ''
      # That's Nix's string interpolation!
      #                         VV.......V
      export GIT_CONFIG_GLOBAL="${gitConf}"
      #                         ^^.......^
      
      # stuff
      ''
  ;
}

(Use ''\{..} for Bash’s string interpolation.)

2 Likes