2023-01-20 16:32:31 +02:00
|
|
|
{ lib, config, nixpkgs, ... }:
|
2019-06-11 22:45:57 +03:00
|
|
|
let
|
2023-01-20 16:32:31 +02:00
|
|
|
pkgs = config.nodes.client.nixpkgs.pkgs;
|
2019-06-11 22:45:57 +03:00
|
|
|
|
2022-10-29 22:51:29 +03:00
|
|
|
# Generate a fake root CA and a fake api.github.com / github.com / channels.nixos.org certificate.
|
2023-01-20 16:32:31 +02:00
|
|
|
cert = pkgs.runCommand "cert" { nativeBuildInputs = [ pkgs.openssl ]; }
|
2019-06-11 22:45:57 +03:00
|
|
|
''
|
|
|
|
mkdir -p $out
|
|
|
|
|
|
|
|
openssl genrsa -out ca.key 2048
|
|
|
|
openssl req -new -x509 -days 36500 -key ca.key \
|
|
|
|
-subj "/C=NL/ST=Denial/L=Springfield/O=Dis/CN=Root CA" -out $out/ca.crt
|
|
|
|
|
|
|
|
openssl req -newkey rsa:2048 -nodes -keyout $out/server.key \
|
|
|
|
-subj "/C=CN/ST=Denial/L=Springfield/O=Dis/CN=github.com" -out server.csr
|
2022-10-29 22:51:29 +03:00
|
|
|
openssl x509 -req -extfile <(printf "subjectAltName=DNS:api.github.com,DNS:github.com,DNS:channels.nixos.org") \
|
2019-06-11 22:45:57 +03:00
|
|
|
-days 36500 -in server.csr -CA $out/ca.crt -CAkey ca.key -CAcreateserial -out $out/server.crt
|
|
|
|
'';
|
|
|
|
|
|
|
|
registry = pkgs.writeTextFile {
|
|
|
|
name = "registry";
|
|
|
|
text = ''
|
|
|
|
{
|
2020-06-03 17:29:04 +03:00
|
|
|
"flakes": [
|
|
|
|
{
|
|
|
|
"from": {
|
|
|
|
"type": "indirect",
|
|
|
|
"id": "nixpkgs"
|
|
|
|
},
|
|
|
|
"to": {
|
|
|
|
"type": "github",
|
|
|
|
"owner": "NixOS",
|
|
|
|
"repo": "nixpkgs"
|
|
|
|
}
|
2022-10-30 08:10:58 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"from": {
|
|
|
|
"type": "indirect",
|
|
|
|
"id": "private-flake"
|
|
|
|
},
|
|
|
|
"to": {
|
|
|
|
"type": "github",
|
|
|
|
"owner": "fancy-enterprise",
|
|
|
|
"repo": "private-flake"
|
|
|
|
}
|
2019-06-11 22:45:57 +03:00
|
|
|
}
|
2020-06-03 17:29:04 +03:00
|
|
|
],
|
|
|
|
"version": 2
|
2019-06-11 22:45:57 +03:00
|
|
|
}
|
|
|
|
'';
|
|
|
|
destination = "/flake-registry.json";
|
|
|
|
};
|
|
|
|
|
2022-10-30 08:10:58 +02:00
|
|
|
private-flake-rev = "9f1dd0df5b54a7dc75b618034482ed42ce34383d";
|
|
|
|
|
|
|
|
private-flake-api = pkgs.runCommand "private-flake" {}
|
|
|
|
''
|
|
|
|
mkdir -p $out/{commits,tarball}
|
|
|
|
|
|
|
|
# Setup https://docs.github.com/en/rest/commits/commits#get-a-commit
|
2024-03-10 14:38:38 +02:00
|
|
|
echo '{"sha": "${private-flake-rev}", "commit": {"tree": {"sha": "ffffffffffffffffffffffffffffffffffffffff"}}}' > $out/commits/HEAD
|
2022-10-30 08:10:58 +02:00
|
|
|
|
|
|
|
# Setup tarball download via API
|
|
|
|
dir=private-flake
|
|
|
|
mkdir $dir
|
|
|
|
echo '{ outputs = {...}: {}; }' > $dir/flake.nix
|
|
|
|
tar cfz $out/tarball/${private-flake-rev} $dir --hard-dereference
|
|
|
|
'';
|
|
|
|
|
|
|
|
nixpkgs-api = pkgs.runCommand "nixpkgs-flake" {}
|
2019-06-11 22:45:57 +03:00
|
|
|
''
|
2022-10-29 22:51:29 +03:00
|
|
|
mkdir -p $out/commits
|
2022-10-30 08:10:58 +02:00
|
|
|
|
|
|
|
# Setup https://docs.github.com/en/rest/commits/commits#get-a-commit
|
2024-03-10 14:38:38 +02:00
|
|
|
echo '{"sha": "${nixpkgs.rev}", "commit": {"tree": {"sha": "ffffffffffffffffffffffffffffffffffffffff"}}}' > $out/commits/HEAD
|
2022-10-29 22:51:29 +03:00
|
|
|
'';
|
|
|
|
|
|
|
|
archive = pkgs.runCommand "nixpkgs-flake" {}
|
|
|
|
''
|
|
|
|
mkdir -p $out/archive
|
2019-11-06 15:48:35 +02:00
|
|
|
|
2019-06-11 22:45:57 +03:00
|
|
|
dir=NixOS-nixpkgs-${nixpkgs.shortRev}
|
|
|
|
cp -prd ${nixpkgs} $dir
|
|
|
|
# Set the correct timestamp in the tarball.
|
2023-09-19 18:21:07 +03:00
|
|
|
find $dir -print0 | xargs -0 touch -h -t ${builtins.substring 0 12 nixpkgs.lastModifiedDate}.${builtins.substring 12 2 nixpkgs.lastModifiedDate} --
|
2022-10-29 22:51:29 +03:00
|
|
|
tar cfz $out/archive/${nixpkgs.rev}.tar.gz $dir --hard-dereference
|
2019-06-11 22:45:57 +03:00
|
|
|
'';
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
2020-10-18 00:34:38 +03:00
|
|
|
name = "github-flakes";
|
2019-06-11 22:45:57 +03:00
|
|
|
|
|
|
|
nodes =
|
2022-06-24 00:56:26 +03:00
|
|
|
{
|
2019-06-11 22:45:57 +03:00
|
|
|
github =
|
|
|
|
{ config, pkgs, ... }:
|
|
|
|
{ networking.firewall.allowedTCPPorts = [ 80 443 ];
|
|
|
|
|
|
|
|
services.httpd.enable = true;
|
|
|
|
services.httpd.adminAddr = "foo@example.org";
|
|
|
|
services.httpd.extraConfig = ''
|
|
|
|
ErrorLog syslog:local6
|
|
|
|
'';
|
2022-06-24 00:56:26 +03:00
|
|
|
services.httpd.virtualHosts."channels.nixos.org" =
|
2020-03-20 16:03:18 +02:00
|
|
|
{ forceSSL = true;
|
|
|
|
sslServerKey = "${cert}/server.key";
|
|
|
|
sslServerCert = "${cert}/server.crt";
|
|
|
|
servedDirs =
|
2022-06-24 00:56:26 +03:00
|
|
|
[ { urlPath = "/";
|
2020-03-20 16:03:18 +02:00
|
|
|
dir = registry;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
services.httpd.virtualHosts."api.github.com" =
|
|
|
|
{ forceSSL = true;
|
|
|
|
sslServerKey = "${cert}/server.key";
|
|
|
|
sslServerCert = "${cert}/server.crt";
|
|
|
|
servedDirs =
|
|
|
|
[ { urlPath = "/repos/NixOS/nixpkgs";
|
2022-10-30 08:10:58 +02:00
|
|
|
dir = nixpkgs-api;
|
|
|
|
}
|
|
|
|
{ urlPath = "/repos/fancy-enterprise/private-flake";
|
|
|
|
dir = private-flake-api;
|
2020-03-20 16:03:18 +02:00
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
2022-10-29 22:51:29 +03:00
|
|
|
services.httpd.virtualHosts."github.com" =
|
|
|
|
{ forceSSL = true;
|
|
|
|
sslServerKey = "${cert}/server.key";
|
|
|
|
sslServerCert = "${cert}/server.crt";
|
|
|
|
servedDirs =
|
|
|
|
[ { urlPath = "/NixOS/nixpkgs";
|
|
|
|
dir = archive;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
2019-06-11 22:45:57 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
client =
|
2019-11-06 15:48:35 +02:00
|
|
|
{ config, lib, pkgs, nodes, ... }:
|
2019-06-11 22:45:57 +03:00
|
|
|
{ virtualisation.writableStore = true;
|
2019-12-18 15:34:03 +02:00
|
|
|
virtualisation.diskSize = 2048;
|
2022-06-07 14:59:36 +03:00
|
|
|
virtualisation.additionalPaths = [ pkgs.hello pkgs.fuse ];
|
2020-03-20 16:03:18 +02:00
|
|
|
virtualisation.memorySize = 4096;
|
2022-12-24 11:14:09 +02:00
|
|
|
nix.settings.substituters = lib.mkForce [ ];
|
2019-11-06 15:48:35 +02:00
|
|
|
nix.extraOptions = "experimental-features = nix-command flakes";
|
2023-12-18 20:19:25 +02:00
|
|
|
networking.hosts.${(builtins.head nodes.github.networking.interfaces.eth1.ipv4.addresses).address} =
|
2022-10-29 22:51:29 +03:00
|
|
|
[ "channels.nixos.org" "api.github.com" "github.com" ];
|
2019-06-11 22:45:57 +03:00
|
|
|
security.pki.certificateFiles = [ "${cert}/ca.crt" ];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-10-17 23:08:18 +03:00
|
|
|
testScript = { nodes }: ''
|
|
|
|
# fmt: off
|
|
|
|
import json
|
|
|
|
import time
|
2019-06-11 22:45:57 +03:00
|
|
|
|
2020-10-17 23:08:18 +03:00
|
|
|
start_all()
|
2019-06-11 22:45:57 +03:00
|
|
|
|
2022-10-30 08:10:58 +02:00
|
|
|
def cat_log():
|
|
|
|
github.succeed("cat /var/log/httpd/*.log >&2")
|
|
|
|
|
2020-10-17 23:08:18 +03:00
|
|
|
github.wait_for_unit("httpd.service")
|
2019-06-11 22:45:57 +03:00
|
|
|
|
2022-10-29 22:51:29 +03:00
|
|
|
client.succeed("curl -v https://github.com/ >&2")
|
2022-10-30 08:10:58 +02:00
|
|
|
out = client.succeed("nix registry list")
|
|
|
|
print(out)
|
|
|
|
assert "github:NixOS/nixpkgs" in out, "nixpkgs flake not found"
|
|
|
|
assert "github:fancy-enterprise/private-flake" in out, "private flake not found"
|
|
|
|
cat_log()
|
|
|
|
|
|
|
|
# If no github access token is provided, nix should use the public archive url...
|
|
|
|
out = client.succeed("nix flake metadata nixpkgs --json")
|
|
|
|
print(out)
|
|
|
|
info = json.loads(out)
|
|
|
|
assert info["revision"] == "${nixpkgs.rev}", f"revision mismatch: {info['revision']} != ${nixpkgs.rev}"
|
|
|
|
cat_log()
|
|
|
|
|
|
|
|
# ... otherwise it should use the API
|
|
|
|
out = client.succeed("nix flake metadata private-flake --json --access-tokens github.com=ghp_000000000000000000000000000000000000 --tarball-ttl 0")
|
|
|
|
print(out)
|
|
|
|
info = json.loads(out)
|
|
|
|
assert info["revision"] == "${private-flake-rev}", f"revision mismatch: {info['revision']} != ${private-flake-rev}"
|
2024-09-11 18:27:39 +03:00
|
|
|
assert info["fingerprint"]
|
2022-10-30 08:10:58 +02:00
|
|
|
cat_log()
|
2019-06-11 22:45:57 +03:00
|
|
|
|
2024-09-11 18:27:39 +03:00
|
|
|
# Fetching with the resolved URL should produce the same result.
|
|
|
|
info2 = json.loads(client.succeed(f"nix flake metadata {info['url']} --json --access-tokens github.com=ghp_000000000000000000000000000000000000 --tarball-ttl 0"))
|
|
|
|
print(info["fingerprint"], info2["fingerprint"])
|
|
|
|
assert info["fingerprint"] == info2["fingerprint"], "fingerprint mismatch"
|
|
|
|
|
2020-10-17 23:08:18 +03:00
|
|
|
client.succeed("nix registry pin nixpkgs")
|
2022-10-30 08:10:58 +02:00
|
|
|
client.succeed("nix flake metadata nixpkgs --tarball-ttl 0 >&2")
|
2019-06-11 22:45:57 +03:00
|
|
|
|
2023-09-28 17:52:28 +03:00
|
|
|
# Test fetchTree on a github URL.
|
2024-03-27 16:53:11 +02:00
|
|
|
hash = client.succeed(f"nix eval --no-trust-tarballs-from-git-forges --raw --expr '(fetchTree {info['url']}).narHash'")
|
2023-09-28 17:52:28 +03:00
|
|
|
assert hash == info['locked']['narHash']
|
|
|
|
|
2024-03-27 16:53:11 +02:00
|
|
|
# Fetching without a narHash should succeed if trust-github is set and fail otherwise.
|
|
|
|
client.succeed(f"nix eval --raw --expr 'builtins.fetchTree github:github:fancy-enterprise/private-flake/{info['revision']}'")
|
|
|
|
out = client.fail(f"nix eval --no-trust-tarballs-from-git-forges --raw --expr 'builtins.fetchTree github:github:fancy-enterprise/private-flake/{info['revision']}' 2>&1")
|
|
|
|
assert "will not fetch unlocked input" in out, "--no-trust-tarballs-from-git-forges did not fail with the expected error"
|
|
|
|
|
2020-10-17 23:08:18 +03:00
|
|
|
# Shut down the web server. The flake should be cached on the client.
|
|
|
|
github.succeed("systemctl stop httpd.service")
|
2019-06-11 22:45:57 +03:00
|
|
|
|
2022-10-30 08:10:58 +02:00
|
|
|
info = json.loads(client.succeed("nix flake metadata nixpkgs --json"))
|
2020-10-17 23:08:18 +03:00
|
|
|
date = time.strftime("%Y%m%d%H%M%S", time.gmtime(info['lastModified']))
|
|
|
|
assert date == "${nixpkgs.lastModifiedDate}", "time mismatch"
|
2019-06-11 22:45:57 +03:00
|
|
|
|
2020-10-17 23:08:18 +03:00
|
|
|
client.succeed("nix build nixpkgs#hello")
|
2019-06-11 22:45:57 +03:00
|
|
|
|
2020-10-17 23:08:18 +03:00
|
|
|
# The build shouldn't fail even with --tarball-ttl 0 (the server
|
|
|
|
# being down should not be a fatal error).
|
|
|
|
client.succeed("nix build nixpkgs#fuse --tarball-ttl 0")
|
|
|
|
'';
|
2019-06-11 22:45:57 +03:00
|
|
|
|
2023-01-20 16:32:31 +02:00
|
|
|
}
|