grace/dots
dotfiles/nix configs
git clone https://git.gae.moe/grace/dots
b70d9f9
main
1{ lib , pkgs , ...} : 2 3let 4gitDir = "/mnt/ext/git" ; 5sorcery = import ../pkgs/sorcery.nix { inherit pkgs ; }; 6auth = import ./nginx-auth.nix { }; 7instances = { 8public = { 9domain = "git.gae.moe" ; 10exports = " ${ gitDir } /srv/public" ; 11repositories = { 12grace = " ${ gitDir } /grace" ; 13}; 14}; 15private = { 16domain = "i.git.gae.moe" ; 17exports = " ${ gitDir } /srv/private" ; 18repositories = { 19private = " ${ gitDir } /private" ; 20classes = " ${ gitDir } /classes" ; 21mirror-gyoder = " ${ gitDir } /mirrors/gyoder" ; 22mirror-very-old = " ${ gitDir } /mirrors/very_old/gyoder" ; 23mirror-autonomous = " ${ gitDir } /mirrors/Autonomous-Motorsports-Purdue" ; 24}; 25}; 26}; 27sshCommand = pkgs . writeShellScript "sorcery-ssh-command" '' 28export PATH = ${ pkgs . git } /bin: $ PATH 29export SORCERY_REPOSITORIES = ${ instances . public . exports } 30export SORCERY_INSTANCE_NAME =git.gae.moe 31export SORCERY_CLONE_URL_BASE =https://git.gae.moe 32umask 0007 33exec ${ sorcery . sshPackage } /bin/sorcery-ssh 34'' ; 35 36mkService = name :instance :{ 37description = "Sorcery ${ name } Git server" ; 38wantedBy = [ "multi-user.target" ]; 39after = [ "systemd-tmpfiles-setup.service" ]; 40unitConfig . RequiresMountsFor = gitDir ; 41path = [ 42pkgs . git 43pkgs . coreutils 44]; 45environment = { 46SORCERY_REPOSITORIES = "/run/sorcery- ${ name } /repositories" ; 47SORCERY_CACHE = "/var/cache/sorcery- ${ name } " ; 48SORCERY_SOCKET = "/run/sorcery- ${ name } /sock" ; 49SORCERY_INSTANCE_NAME = instance . domain ; 50SORCERY_CLONE_URL_BASE = "https:// ${ instance . domain } " ; 51}; 52preStart = '' 53mkdir -p /var/cache/sorcery- ${ name } /.arborium/ ${ sorcery . grammarVersion } 54cp -L --remove-destination ${ sorcery . grammars } /*.wasm /var/cache/sorcery- ${ name } /.arborium/ ${ sorcery . grammarVersion } / 55'' ; 56serviceConfig = { 57ExecStart = " ${ sorcery . package } /bin/sorceryd" ; 58User = "git" ; 59Group = "git" ; 60UMask = "0007" ; 61RuntimeDirectory = "sorcery- ${ name } " ; 62RuntimeDirectoryMode = "0750" ; 63CacheDirectory = "sorcery- ${ name } " ; 64CacheDirectoryMode = "0750" ; 65BindPaths = lib . mapAttrsToList ( 66namespace :path :"- ${ path } :/run/sorcery- ${ name } /repositories/ ${ namespace } " 67) instance . repositories ; 68Restart = "on-failure" ; 69RestartSec = 2 ; 70NoNewPrivileges = true ; 71PrivateTmp = true ; 72ProtectSystem = "strict" ; 73ProtectHome = "read-only" ; 74}; 75}; 76 77mkHost = name :instance :{ 78locations = 79( lib . optionalAttrs ( name == "private" ) auth . locations ) 80// { 81"/" = { 82proxyPass = "http://unix:/run/sorcery- ${ name } /sock" ; 83extraConfig = lib . optionalString ( name == "private" ) auth . browser ; 84}; 85"~ ^/_sorcery/(.+/(?:refs|ref/.+/blob/.+))$" . extraConfig = '' 86internal; 87auth_request off; 88alias /var/cache/sorcery- ${ name } /$1; 89types { } 90default_type text/html; 91'' ; 92"/_sorcery/" . extraConfig = '' 93internal; 94auth_request off; 95alias /var/cache/sorcery- ${ name } /; 96'' ; 97"/_sorcery-git/" . extraConfig = '' 98internal; 99auth_request off; 100alias ${ instance . exports } /; 101'' ; 102} 103// lib . optionalAttrs ( name == "private" ) { 104"~ ^/[^/]+/[^/]+/(info/refs|git-upload-pack)$" = { 105proxyPass = "http://unix:/run/sorcery-private/sock" ; 106extraConfig = auth . git ; 107}; 108}; 109}; 110in 111{ 112 113imports = [ ./githooks.nix ]; 114programs . git = { 115enable = true ; 116config . init . defaultBranch = "main" ; 117}; 118users . groups . git = { }; 119users . users . git = { 120isSystemUser = true ; 121group = "git" ; 122home = gitDir ; 123shell = pkgs . bashInteractive ; 124}; 125users . users . nginx . extraGroups = [ "git" ]; 126 127environment . etc . "ssh/git-user-ca.pub" . source = ../ssh-ca.pub ; 128environment . etc . "ssh/git-principals" . text = '' 129t:dev 130u:grace 131u:root 132'' ; 133environment . etc . "ssh/git-principals" . mode = "0644" ; 134environment . etc . "ssh/git-authorized-keys" . text = '' 135restrict sk-ssh-ed25519@openssh.com AAAAGnNrLXNzaC1lZDI1NTE5QG9wZW5zc2guY29tAAAAIA6Eq2yVsZrCH8so7f3ygH0UWp6WkOxbhLNFC9QewKwoAAAABHNzaDo= grace_yoder@outlook.com 136'' ; 137environment . etc . "ssh/git-authorized-keys" . mode = "0644" ; 138services . openssh = { 139enable = true ; 140extraConfig = '' 141Match User git 142TrustedUserCAKeys /etc/ssh/git-user-ca.pub 143AuthorizedPrincipalsFile /etc/ssh/git-principals 144AuthorizedKeysFile /etc/ssh/git-authorized-keys 145ForceCommand ${ sshCommand } 146PubkeyAuthentication yes 147PasswordAuthentication no 148KbdInteractiveAuthentication no 149DisableForwarding yes 150PermitTTY no 151PermitUserRC no 152X11Forwarding no 153'' ; 154}; 155 156systemd . tmpfiles . rules = [ 157"d ${ gitDir } 0750 git git -" 158"d ${ gitDir } /mirrors 0750 git git -" 159"d ${ gitDir } /mirrors/very_old 0750 git git -" 160"d ${ gitDir } /srv 0750 git git -" 161] 162++ lib . concatLists ( 163lib . mapAttrsToList ( 164_ :instance : 165[ "d ${ instance . exports } 0750 git git -" ] 166++ lib . mapAttrsToList ( _ :path :"d ${ path } 0750 git git -" ) instance . repositories 167++ lib . mapAttrsToList ( 168namespace :path :"L+ ${ instance . exports } / ${ namespace } - - - - ${ path } " 169) instance . repositories 170) instances 171); 172 173systemd . services = lib . mapAttrs' ( 174name :instance :lib . nameValuePair "sorcery- ${ name } " ( mkService name instance ) 175) instances ; 176services . nginx . virtualHosts = lib . mapAttrs' ( 177name :instance :lib . nameValuePair instance . domain ( mkHost name instance ) 178) instances ; 179}