Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add nix backend #20873

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Empty file.
Empty file.
64 changes: 64 additions & 0 deletions src/python/pants/backend/nix/goals/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import os

from pants.backend.nix.target_types import NixBinaryExprField, NixBinaryRelativePathField
from pants.core.goals.run import RunFieldSet, RunInSandboxBehavior, RunRequest
from pants.core.util_rules.system_binaries import SystemBinariesSubsystem
from pants.engine.fs import EMPTY_DIGEST
from pants.engine.process import Process, ProcessResult
from pants.engine.rules import Get, collect_rules, rule
from pants.engine.target import WrappedTarget, WrappedTargetRequest


class NixBinaryFieldSet(RunFieldSet):
required_fields = (
NixBinaryExprField,
NixBinaryRelativePathField,
)
run_in_sandbox_behavior = RunInSandboxBehavior.RUN_REQUEST_HERMETIC


@rule
async def run_nix_binary(
field_set: NixBinaryFieldSet,
system_binaries: SystemBinariesSubsystem.EnvironmentAware,
) -> RunRequest:
wrapped_tgt = await Get(
WrappedTarget,
WrappedTargetRequest(field_set.address, description_of_origin="<infallible>"),
)
expr = wrapped_tgt.target[NixBinaryExprField].value
assert expr
rel_path = wrapped_tgt.target[NixBinaryRelativePathField].value
assert rel_path

# TODO search path
nix_instantiate = "/nix/var/nix/profiles/default/bin/nix-instantiate"
instantiate_result = await Get(
ProcessResult,
Process(
argv=(nix_instantiate, "--expr", expr),
description="Create nix .drv file",
),
)
drv_path = instantiate_result.stdout.decode("utf-8").strip()

# TODO search path
nix_store = "/nix/var/nix/profiles/default/bin/nix-store"
realise_result = await Get(
ProcessResult,
Process(
argv=(nix_store, "--realise", drv_path),
description="Build nix derivation",
),
)
derivation_dir = realise_result.stdout.decode("utf-8").strip()

binary_path = os.path.join(derivation_dir, rel_path)
return RunRequest(args=[binary_path], digest=EMPTY_DIGEST)


def rules():
return [
*collect_rules(),
*NixBinaryFieldSet.rules(),
]
10 changes: 10 additions & 0 deletions src/python/pants/backend/nix/register.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from pants.backend.nix.goals import run
from pants.backend.nix.target_types import NixBinaryTarget, NixSourceTarget


def target_types():
return [NixBinaryTarget, NixSourceTarget]


def rules():
return [*run.rules()]
50 changes: 50 additions & 0 deletions src/python/pants/backend/nix/target_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from pants.engine.target import (
COMMON_TARGET_FIELDS,
Dependencies,
SingleSourceField,
StringField,
Target,
)


class NixSourceField(SingleSourceField):
pass


class NixDependenciesField(Dependencies):
pass


class NixSourceTarget(Target):
alias = "nix_source"
core_fields = (
*COMMON_TARGET_FIELDS,
NixSourceField,
NixDependenciesField,
)


class NixBinaryExprField(StringField):
alias = "expr"
required = True # TODO make expr optional
help = "Passed to nix-instantiate --expr."


class NixBinaryRelativePathField(StringField):
alias = "rel_path"
required = True
help = "Binary path relative to derivation root."


class NixBinaryDependencies(Dependencies):
pass


class NixBinaryTarget(Target):
alias = "nix_binary"
core_fields = (
*COMMON_TARGET_FIELDS,
NixBinaryExprField,
NixBinaryDependencies,
NixBinaryRelativePathField,
)