Skip to content

Instantly share code, notes, and snippets.

@arianvp
Last active February 13, 2025 22:15
Show Gist options
  • Save arianvp/2fecd5fb62e2dbe38a2a25bf9e2d50b5 to your computer and use it in GitHub Desktop.
Save arianvp/2fecd5fb62e2dbe38a2a25bf9e2d50b5 to your computer and use it in GitHub Desktop.

Asynchronous post-build-hook

this hook queues up store paths on a unix domain socket and then spawns a service to actually push the store path to the cache through socket activation

works on MacOS and Linux

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>nix-post-build-hook</string>
<key>KeepAlive</key>
<false/>
<key>RunAtLoad</key>
<true/>
<key>Program></key>
<string>xargs</string>
<key>ProgramArguments</key>
<array>
<string>--verbose</string>
<string>--no-run-if-empty</string>
<string>nix</string>
<string>copy</string>
<string>--to</string>
<string>s3://my-bucket</string>
</array>
<key>StandardErrorPath</key>
<string>/var/log/nix-post-build-hook.log</string>
<key>Sockets</key>
<dict>
<key>Listeners</key>
<dict>
<key>SocketType</key>
<string>stream</string>
<key>SockPathName</key>
<string>/var/run/nix-post-build-hook.sock</string>
<key>inetdCompatibility</key>
<dict>
<key>Wait</key>
<false/>
</dict>
</dict>
</dict>
</dict>
</plist>
[Socket]
ListenStream=%t/%N.sock
SocketMode=0600
Accept=yes
[Service]
LoadCredential=signing-key
Environment=NIX_STORE_URI=s3://my-bucket?secret-key=%d/signing-key&compression=zstd&parallel-compression=true
StandardInput=socket
ExecSearchPath=/nix/var/nix/profiles/default/bin:/run/current-system/sw/bin
ExecStart=xargs --no-run-if-empty --verbose nix copy --to $NIX_STORE_URI
DynamicUser=yes
post-build-hook = socat EXEC:'printenv OUT_PATHS' UNIX-CONNECT:/var/run/nix-copy.sock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment