2023-09-19 19:48:39 +00:00
|
|
|
import json
|
2023-07-26 06:00:07 +00:00
|
|
|
import os
|
2023-08-26 08:32:04 +00:00
|
|
|
import tempfile
|
2023-10-12 20:46:32 +00:00
|
|
|
from pathlib import Path
|
2023-09-19 19:48:39 +00:00
|
|
|
from typing import Any
|
2023-07-26 06:00:07 +00:00
|
|
|
|
2024-06-13 22:44:50 +00:00
|
|
|
from .cmd import run, run_no_stdout
|
2023-09-26 15:31:45 +00:00
|
|
|
from .dirs import nixpkgs_flake, nixpkgs_source
|
2023-11-21 17:13:30 +00:00
|
|
|
|
2023-09-13 14:01:03 +00:00
|
|
|
|
2023-09-15 14:22:05 +00:00
|
|
|
def nix_command(flags: list[str]) -> list[str]:
|
2023-11-29 11:38:00 +00:00
|
|
|
return ["nix", "--extra-experimental-features", "nix-command flakes", *flags]
|
2023-09-15 14:22:05 +00:00
|
|
|
|
|
|
|
|
2023-11-21 10:36:50 +00:00
|
|
|
def nix_flake_show(flake_url: str | Path) -> list[str]:
|
2023-10-01 10:45:01 +00:00
|
|
|
return nix_command(
|
|
|
|
[
|
|
|
|
"flake",
|
|
|
|
"show",
|
|
|
|
"--json",
|
|
|
|
"--show-trace",
|
2023-10-31 18:05:07 +00:00
|
|
|
"--no-write-lock-file",
|
2023-10-01 10:45:01 +00:00
|
|
|
f"{flake_url}",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2023-11-21 17:13:30 +00:00
|
|
|
|
2023-12-30 23:49:57 +00:00
|
|
|
def nix_build(flags: list[str], gcroot: Path | None = None) -> list[str]:
|
|
|
|
if gcroot is not None:
|
|
|
|
return (
|
|
|
|
nix_command(
|
|
|
|
[
|
|
|
|
"build",
|
|
|
|
"--out-link",
|
|
|
|
str(gcroot),
|
|
|
|
"--print-out-paths",
|
|
|
|
"--no-write-lock-file",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
+ flags
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
return (
|
|
|
|
nix_command(
|
|
|
|
[
|
|
|
|
"build",
|
|
|
|
"--no-link",
|
|
|
|
"--print-out-paths",
|
|
|
|
"--no-write-lock-file",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
+ flags
|
2023-09-15 14:22:05 +00:00
|
|
|
)
|
2023-08-26 09:23:15 +00:00
|
|
|
|
2023-11-21 17:13:30 +00:00
|
|
|
|
2024-02-06 12:25:34 +00:00
|
|
|
def nix_add_to_gcroots(nix_path: Path, dest: Path) -> None:
|
|
|
|
cmd = ["nix-store", "--realise", f"{nix_path}", "--add-root", f"{dest}"]
|
|
|
|
run(cmd)
|
|
|
|
|
|
|
|
|
2023-09-19 19:48:39 +00:00
|
|
|
def nix_config() -> dict[str, Any]:
|
2024-06-13 22:44:50 +00:00
|
|
|
cmd = nix_command(["show-config", "--json"])
|
|
|
|
proc = run_no_stdout(cmd)
|
2023-09-19 19:48:39 +00:00
|
|
|
data = json.loads(proc.stdout)
|
|
|
|
config = {}
|
|
|
|
for key, value in data.items():
|
|
|
|
config[key] = value["value"]
|
|
|
|
return config
|
|
|
|
|
2023-11-21 17:13:30 +00:00
|
|
|
|
2023-08-26 08:32:04 +00:00
|
|
|
def nix_eval(flags: list[str]) -> list[str]:
|
2023-09-15 14:22:05 +00:00
|
|
|
default_flags = nix_command(
|
|
|
|
[
|
|
|
|
"eval",
|
|
|
|
"--show-trace",
|
|
|
|
"--json",
|
2023-10-31 18:05:07 +00:00
|
|
|
"--no-write-lock-file",
|
2023-09-15 14:22:05 +00:00
|
|
|
]
|
|
|
|
)
|
2023-09-15 11:44:04 +00:00
|
|
|
if os.environ.get("IN_NIX_SANDBOX"):
|
|
|
|
with tempfile.TemporaryDirectory() as nix_store:
|
2023-11-29 11:38:00 +00:00
|
|
|
return [
|
|
|
|
*default_flags,
|
|
|
|
"--override-input",
|
|
|
|
"nixpkgs",
|
|
|
|
str(nixpkgs_source()),
|
|
|
|
# --store is required to prevent this error:
|
|
|
|
# error: cannot unlink '/nix/store/6xg259477c90a229xwmb53pdfkn6ig3g-default-builder.sh': Operation not permitted
|
|
|
|
"--store",
|
|
|
|
nix_store,
|
|
|
|
*flags,
|
|
|
|
]
|
2023-09-15 11:44:04 +00:00
|
|
|
return default_flags + flags
|
2023-08-26 08:32:04 +00:00
|
|
|
|
2023-11-21 17:13:30 +00:00
|
|
|
|
2023-12-08 23:04:56 +00:00
|
|
|
def nix_metadata(flake_url: str | Path) -> dict[str, Any]:
|
|
|
|
cmd = nix_command(["flake", "metadata", "--json", f"{flake_url}"])
|
2024-01-03 13:25:34 +00:00
|
|
|
proc = run(cmd)
|
2023-12-08 12:46:21 +00:00
|
|
|
data = json.loads(proc.stdout)
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
2023-07-26 06:00:07 +00:00
|
|
|
def nix_shell(packages: list[str], cmd: list[str]) -> list[str]:
|
2023-08-26 09:23:15 +00:00
|
|
|
# we cannot use nix-shell inside the nix sandbox
|
|
|
|
# in our tests we just make sure we have all the packages
|
|
|
|
if os.environ.get("IN_NIX_SANDBOX"):
|
|
|
|
return cmd
|
2023-12-08 14:00:11 +00:00
|
|
|
return [
|
2024-05-27 11:40:03 +00:00
|
|
|
*nix_command(["shell", "--inputs-from", f"{nixpkgs_flake()!s}"]),
|
2023-12-08 14:00:11 +00:00
|
|
|
*packages,
|
|
|
|
"-c",
|
|
|
|
*cmd,
|
|
|
|
]
|