Skip to content

Commit d00e66b

Browse files
committed
added script to upload modules to binary cache; split out lkl
1 parent 1bc1a6c commit d00e66b

8 files changed

+119
-10
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ result
22
build
33
.envrc
44
.direnv
5+
.netrc
6+
nix_build_cache_signing_key

.semaphore/build-nixmodules.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ auto_cancel:
1414
global_job_config:
1515
secrets:
1616
- name: gcp-goval
17+
- name: nix-build-cache-auth
1718
env_vars:
1819
- name: NIX_FLAGS
1920
value: "--extra-experimental-features nix-command --extra-experimental-features flakes --extra-experimental-features discard-references"
@@ -34,7 +35,8 @@ blocks:
3435
jobs:
3536
- name: build nixmodules disk image
3637
commands:
37-
- ./scripts/build_disk_image.sh > nixmodules_build.log 2>&1
38+
- scripts/setup_nix_binary_cache.py
39+
- scripts/build_disk_image.sh 2>&1 | tee nixmodules_build.log
3840
epilogue:
3941
always:
4042
commands:

pkgs/bundle-image/default.nix

+2-9
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,13 @@
1212
, upgrade-maps
1313
, active-modules
1414
, fetchFromGitHub
15+
, pkgs
1516
}:
1617

1718
let
1819
label = "nixmodules-${revstring}";
1920
registry = ../../modules.json;
20-
# wating for upstream to include our patch: https://github.com/lkl/linux/pull/532 and https://github.com/lkl/linux/pull/534
21-
lkl' = lkl.overrideAttrs (oldAttrs: {
22-
src = fetchFromGitHub {
23-
owner = "numtide";
24-
repo = "linux-lkl";
25-
rev = "2cbcbd26044f72e47740588cfa21bf0e7b698262";
26-
sha256 = "sha256-i7uc69bF84kkS7MahpgJ2EnWZLNah+Ees2oRGMzIee0=";
27-
};
28-
});
21+
lkl' = pkgs.callPackage ../lkl { };
2922
in
3023

3124
derivation {

pkgs/default.nix

+11
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ rec {
5757

5858
rev_long = pkgs.writeText "rev_long" revstring_long;
5959

60+
lkl = pkgs.callPackage ./lkl { };
61+
6062
active-modules = import ./active-modules {
6163
inherit pkgs;
6264
inherit self;
@@ -81,6 +83,15 @@ rec {
8183
};
8284
};
8385

86+
all-historical-modules = mapAttrs
87+
(moduleId: module:
88+
let
89+
flake = builtins.getFlake "github:replit/nixmodules/${module.commit}";
90+
shortModuleId = elemAt (strings.splitString ":" moduleId) 0;
91+
in
92+
flake.modules.${shortModuleId})
93+
all-modules;
94+
8495
bundle-squashfs = bundle-squashfs-fn {
8596
moduleIds = [ "python-3.10" "nodejs-18" ];
8697
inherit upgrade-maps;

pkgs/lkl/default.nix

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# waiting for upstream to include our patch: https://github.com/lkl/linux/pull/532 and https://github.com/lkl/linux/pull/534
2+
{ lkl, fetchFromGitHub }:
3+
lkl.overrideAttrs (oldAttrs: {
4+
src = fetchFromGitHub {
5+
owner = "numtide";
6+
repo = "linux-lkl";
7+
rev = "2cbcbd26044f72e47740588cfa21bf0e7b698262";
8+
sha256 = "sha256-i7uc69bF84kkS7MahpgJ2EnWZLNah+Ees2oRGMzIee0=";
9+
};
10+
})

scripts/cache_all_modules.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import json
2+
import subprocess
3+
4+
def main():
5+
file = open('modules.json')
6+
modules = json.load(file)
7+
file.close()
8+
for module_id in modules.keys():
9+
cmd = [
10+
'scripts/cache_nix_build.sh', '.#all-historical-modules."%s"' % module_id
11+
]
12+
print(" ".join(cmd))
13+
subprocess.check_output(cmd, stderr=subprocess.PIPE)
14+
print('%s done' % " ".join(cmd))
15+
16+
if __name__ == '__main__':
17+
main()

scripts/cache_nix_build.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
# Builds and uploads a nix package and its run-time and build-time dependencies to our Nix cahce.
3+
4+
set -eo pipefail
5+
6+
nix build $@
7+
8+
nix-store -qR --include-outputs $(nix-store -qd $(nix path-info $@)) \
9+
| grep -v '\.drv$' \
10+
| xargs nix copy --to https://nix-build-cache.replit.com?secret-key=./nix_build_cache_signing_key
11+
12+

scripts/setup_nix_binary_cache.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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

Comments
 (0)