tests/functional2: migrate why-depends.sh

Make builds work on darwin and migrate the first building test :D

Change-Id: Ia301c78d3bc771cd826d93dc114b098167043e97
This commit is contained in:
Commentator2.0
2025-09-19 20:51:49 +02:00
parent a25a5739c7
commit fc2eedf9db
5 changed files with 103 additions and 46 deletions
+24 -4
View File
@@ -1,8 +1,9 @@
import dataclasses
import sys
from functools import partialmethod
from pathlib import Path
from textwrap import dedent
from typing import Any
from typing import Any, Literal
from collections.abc import Callable, Generator
import pytest
@@ -96,19 +97,38 @@ class Nix:
return self._settings
def nix_cmd(self, argv: list[str], flake: bool = False) -> Command:
def nix_cmd(
self, argv: list[str], flake: bool = False, build: bool | Literal["auto"] = "auto"
) -> Command:
"""
Constructs a NixCommand with the appropriate settings.
:param build: if the executed command wants to build stuff. This is required due to darwin shenanigans. "auto" will try to autodetect, override using `True` or `False`. Has no effect on linux.
"""
# Create a copy of settings to not have a writing side effect
settings = dataclasses.replace(self.settings)
if flake:
settings.feature("nix-command", "flakes")
# FIXME(Commentator2.0): Darwin needs special handling here, as it does not support (non-root) chroots...
# Hence, it cannot build using a relocated store so we just use the local (aka global) store instead
# This is kinda ugly but what else can one do
if sys.platform == "darwin":
if build is True or (
build == "auto" and (argv[0] == "nix-build" or argv[1] == "build")
):
settings.store = None
settings.nix_store_dir = self.env.dirs.nix_store_dir
settings.to_env_overlay(self.env)
return Command(argv=argv, _env=self.env)
def nix(self, cmd: list[str], nix_exe: str = "nix", flake: bool = False) -> Command:
return self.nix_cmd([nix_exe, *cmd], flake=flake)
def nix(
self,
cmd: list[str],
nix_exe: str = "nix",
flake: bool = False,
build: bool | Literal["auto"] = "auto",
) -> Command:
return self.nix_cmd([nix_exe, *cmd], flake=flake, build=build)
# Mark each of these as correct as they are not ClassVars, but we also don't want to turn off RUF045
nix_build = partialmethod(nix, nix_exe="nix-build") # noqa: RUF045
@@ -20,9 +20,15 @@ let
head -c 100k /dev/zero > $out/filler
echo BAR > $out/bar
echo ${input0} > $out/input0
echo ${input3} > $out/input3
'';
};
input3 = mkDerivation {
name = "dependencies-input-3";
buildCommand = "mkdir $out; echo FOO > $out/foo";
};
fod_input = mkDerivation {
name = "fod-input";
buildCommand = ''
@@ -40,8 +46,10 @@ mkDerivation {
builder = ./dependencies.builder0.sh + "/FOOBAR/../.";
input1 = input1 + "/.";
input2 = "${input2}/.";
input3 = "${input3}/.";
input1_drv = input1;
input2_drv = input2;
input3_drv = input3;
input0_drv = input0;
fod_input_drv = fod_input;
meta.description = "Random test package";