2024-04-13 15:28:31 +00:00
|
|
|
{ flake-utils, pkgs, ... }: flake-utils.lib.eachDefaultSystem (system:
|
2024-03-27 19:10:14 +00:00
|
|
|
let
|
2024-05-19 12:05:20 +00:00
|
|
|
createScript = { name, runtimeInputs, scriptPath, extraWrapperFlags ? "", ... }:
|
2024-03-27 19:10:14 +00:00
|
|
|
let
|
2024-04-13 15:28:31 +00:00
|
|
|
script = (pkgs.writeScriptBin name (builtins.readFile scriptPath)).overrideAttrs (old: {
|
2024-03-27 19:10:14 +00:00
|
|
|
buildCommand = "${old.buildCommand}\n patchShebangs $out";
|
|
|
|
});
|
|
|
|
in
|
2024-04-13 15:28:31 +00:00
|
|
|
pkgs.symlinkJoin {
|
2024-03-27 19:10:14 +00:00
|
|
|
inherit name;
|
|
|
|
paths = [ script ] ++ runtimeInputs;
|
2024-04-13 15:28:31 +00:00
|
|
|
buildInputs = [ pkgs.makeWrapper ];
|
2024-05-19 12:05:20 +00:00
|
|
|
postBuild = "wrapProgram $out/bin/${name} --set PATH $out/bin ${extraWrapperFlags}";
|
2024-03-27 19:10:14 +00:00
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
2024-05-19 12:05:20 +00:00
|
|
|
packages.bootstrap = createScript {
|
|
|
|
name = "bootstrap";
|
2024-06-15 20:27:07 +00:00
|
|
|
runtimeInputs = with pkgs; [ sops coreutils nixos-anywhere ];
|
2024-05-19 12:05:20 +00:00
|
|
|
scriptPath = ./bootstrap.sh;
|
|
|
|
};
|
|
|
|
|
|
|
|
packages.gen-k3s-cert = createScript {
|
|
|
|
name = "create-k3s-cert";
|
|
|
|
runtimeInputs = with pkgs; [ openssl coreutils openssh yq ];
|
|
|
|
scriptPath = ./gen-k3s-cert.sh;
|
|
|
|
};
|
2024-06-30 12:35:47 +00:00
|
|
|
|
|
|
|
packages.prefetch-container-images =
|
|
|
|
let
|
|
|
|
images = {
|
|
|
|
cyberchef = {
|
2024-08-25 15:04:31 +00:00
|
|
|
name = "mpepping/cyberchef";
|
|
|
|
tag = "latest";
|
|
|
|
};
|
|
|
|
|
|
|
|
radicale = {
|
|
|
|
name = "tomsquest/docker-radicale";
|
|
|
|
tag = "3.2.2.0";
|
2024-06-30 12:35:47 +00:00
|
|
|
};
|
|
|
|
};
|
2024-08-25 15:04:31 +00:00
|
|
|
|
2024-06-30 12:35:47 +00:00
|
|
|
imagesJSON = builtins.toFile "images.json" (builtins.toJSON images);
|
|
|
|
in
|
|
|
|
pkgs.writers.writePython3Bin "prefetch-container-images"
|
|
|
|
{ } ''
|
|
|
|
import json
|
|
|
|
import subprocess
|
|
|
|
import tempfile
|
|
|
|
import sys
|
|
|
|
from collections import defaultdict
|
|
|
|
|
|
|
|
prefetch_docker_cmd = "${pkgs.lib.getExe pkgs.nix-prefetch-docker}" # noqa: E501
|
|
|
|
nix_cmd = "${pkgs.lib.getExe pkgs.nix}" # noqa: E501
|
|
|
|
images_file_name = "${imagesJSON}"
|
|
|
|
|
|
|
|
results = defaultdict(lambda: defaultdict(dict))
|
|
|
|
|
|
|
|
with open(images_file_name, 'r') as file:
|
|
|
|
data = json.load(file)
|
|
|
|
|
2024-08-25 15:04:31 +00:00
|
|
|
for image_name, image in data.items():
|
|
|
|
name = image["name"]
|
|
|
|
tag = image["tag"]
|
2024-06-30 12:35:47 +00:00
|
|
|
|
2024-08-25 15:04:31 +00:00
|
|
|
print(f"Prefetching image {name}:{tag}", file=sys.stderr)
|
2024-06-30 12:35:47 +00:00
|
|
|
|
2024-08-25 15:04:31 +00:00
|
|
|
prefetch_args = [
|
|
|
|
prefetch_docker_cmd,
|
|
|
|
"--os", "linux",
|
|
|
|
"--arch", "amd64",
|
|
|
|
"--image-name", name,
|
|
|
|
"--image-tag", tag,
|
|
|
|
"--json",
|
|
|
|
"--quiet"
|
|
|
|
]
|
|
|
|
result = subprocess.run(prefetch_args,
|
|
|
|
check=True,
|
|
|
|
capture_output=True,
|
|
|
|
text=True)
|
2024-06-30 12:35:47 +00:00
|
|
|
|
2024-08-25 15:04:31 +00:00
|
|
|
prefetch_data = json.loads(result.stdout)
|
|
|
|
results[image_name] = prefetch_data
|
2024-06-30 12:35:47 +00:00
|
|
|
|
|
|
|
with tempfile.NamedTemporaryFile(mode='w+', suffix='.json') as temp_file:
|
|
|
|
json.dump(results, temp_file, indent=4)
|
|
|
|
temp_file.flush()
|
|
|
|
|
|
|
|
to_nix_args = [
|
|
|
|
nix_cmd,
|
|
|
|
"eval",
|
|
|
|
"--impure",
|
|
|
|
"--expr", f'builtins.fromJSON (builtins.readFile {temp_file.name})'
|
|
|
|
]
|
|
|
|
result = subprocess.run(to_nix_args,
|
|
|
|
check=True,
|
|
|
|
capture_output=True,
|
|
|
|
text=True)
|
|
|
|
|
|
|
|
print(result.stdout)
|
|
|
|
'';
|
2024-03-27 19:10:14 +00:00
|
|
|
})
|