Git: Difference between revisions

imported>RichardJActon
Add an example of setting a simple option in gitconfig with an attribute set in extraConfig
imported>Erikarvstedt
Add section "Serve Git repos via SSH"
Line 105: Line 105:


worked well on a machine with about 6–8 GB of free RAM and two processor threads, and reduced the size of the <code>nixpkgs</code> checkout from ~1.3 GB to ~0.95 GB.
worked well on a machine with about 6–8 GB of free RAM and two processor threads, and reduced the size of the <code>nixpkgs</code> checkout from ~1.3 GB to ~0.95 GB.
= Serve Git repos via SSH =
This section implements [https://git-scm.com/book/en/v2/Git-on-the-Server-Setting-Up-the-Server Git on the Server - Setting Up the Server] on NixOS.
== Configuration ==
<syntaxhighlight lang="nix">
{ config, pkgs, ... }: {
  users.users.git = {
    isSystemUser = true;
    group = "git";
    home = "/var/lib/git-server";
    createHome = true;
    shell = "${pkgs.git}/bin/git-shell";
    openssh.authorizedKeys.keys = [
      # FIXME: Add pubkeys of authorized users
      "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIF38sHxXn/r7KzWL1BVCqcKqmZA/V76N/y5p52UQghw7 example"
    ];
  };
  users.groups.git = {};
  services.openssh = {
    enable = true;
    extraConfig = ''
      Match user git
        AllowTcpForwarding no
        AllowAgentForwarding no
        PasswordAuthentication no
        PermitTTY no
        X11Forwarding no
    '';
  };
}
</syntaxhighlight>
== Usage ==
1. Run this on the server to create repo <code>myproject</code> accessible by user <code>git</code>.
<syntaxhighlight lang="bash">
sudo -u git bash -c '
  cd /var/lib/git-server
  mkdir myproject.git
  cd myproject.git
  git init --bare
'
</syntaxhighlight>
2. Push to the server repo from another system
<syntaxhighlight lang="bash">
mkdir myproject1
cd myproject1
echo hello > a
git init
git add .
git commit -m init
git remote add origin git@myserver:myproject.git
git push origin master
</syntaxhighlight>
3. Clone and edit the server repo from another system
<syntaxhighlight lang="bash">
git clone git@myserver:myproject.git
cd myproject
cat a
echo world >> a
git commit -am hello
git push origin master
</syntaxhighlight>


[[Category:Applications]]
[[Category:Applications]]