|
| 1 | +#! /usr/bin/env python3 |
| 2 | +import os |
| 3 | +import pathlib |
| 4 | + |
| 5 | +def main(): |
| 6 | + auth = get_auth() |
| 7 | + net_rc_path = create_netrc(auth) |
| 8 | + create_signing_key_file(auth) |
| 9 | + create_nix_conf(auth, net_rc_path) |
| 10 | + |
| 11 | +def create_signing_key_file(auth): |
| 12 | + file_contents = "replit-internal-nixcache:%s" % auth["signingKey"] |
| 13 | + signing_key_file_path = os.path.abspath("nix_build_cache_signing_key") |
| 14 | + f = open(signing_key_file_path, "w") |
| 15 | + f.write(file_contents) |
| 16 | + f.close() |
| 17 | + print("Wrote %s" % signing_key_file_path) |
| 18 | + |
| 19 | +def create_nix_conf(auth, netrc_path): |
| 20 | + homedir = os.getenv("HOME") |
| 21 | + nix_conf = "\n".join([ |
| 22 | + "substituters = https://cache.nixos.org/ https://nix-build-cache.replit.com/", |
| 23 | + "trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= replit-internal-nixcache:%s" % auth["publicKey"], |
| 24 | + "netrc-file = %s" % netrc_path, |
| 25 | + "experimental-features = nix-command flakes discard-references" |
| 26 | + ]) |
| 27 | + nix_conf_dir_path = os.path.abspath("%s/.config/nix/" % homedir) |
| 28 | + pathlib.Path(nix_conf_dir_path).mkdir(parents=True, exist_ok=True) |
| 29 | + nix_conf_path = os.path.abspath("%s/nix.conf" % nix_conf_dir_path) |
| 30 | + f = open(nix_conf_path, "w") |
| 31 | + f.write(nix_conf) |
| 32 | + f.close() |
| 33 | + print("Wrote %s" % nix_conf_path) |
| 34 | + |
| 35 | +def create_netrc(auth): |
| 36 | + netrc = "\n".join([ |
| 37 | + "machine nix-build-cache.replit.com", |
| 38 | + "login %s" % auth["login"], |
| 39 | + "password %s" % auth["password"], |
| 40 | + "" |
| 41 | + ]) |
| 42 | + netrc_path = os.path.abspath(".netrc") |
| 43 | + f = open(netrc_path, "w") |
| 44 | + f.write(netrc) |
| 45 | + f.close() |
| 46 | + print("Wrote %s" % netrc_path) |
| 47 | + return netrc_path |
| 48 | + |
| 49 | +def get_auth(): |
| 50 | + login = os.getenv('NIX_BUILD_CACHE_LOGIN') |
| 51 | + password = os.getenv('NIX_BUILD_CACHE_PASSWORD') |
| 52 | + publicKey = os.getenv('NIX_BUILD_CACHE_PUBLIC_KEY') |
| 53 | + signingKey = os.getenv('NIX_BUILD_CACHE_SIGNING_KEY') |
| 54 | + return { |
| 55 | + 'login': login, |
| 56 | + 'password': password, |
| 57 | + 'publicKey': publicKey, |
| 58 | + 'signingKey': signingKey |
| 59 | + } |
| 60 | + |
| 61 | +if __name__ == '__main__': |
| 62 | + main() |
0 commit comments