Use Dynamic Derivations¶
A flake with derivations generated at build time, built and cached by Gradient.
Requirements:
- A task on a flake
- Workers with the
evalandbuildcapabilities
1. Enable the Nix Features¶
The worker module came with ca-derivations already on. Enable dynamic-derivations on every worker host.
-
Worker module features can merge with this list in NixOS.
-
Worker hosts keep their features in
nix.conf. Evaluators read that file. - The
nix-daemoncan reject a generated derivation without the feature.
2. Write a Dynamic Derivation¶
Dynamic derivations need a .drv file. Another derivation made that file as output.
# flake.nix
{
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
outputs = { nixpkgs, ... }: let
pkgs = nixpkgs.legacyPackages.x86_64-linux;
contentAddressed = mode: { # (1)!
__contentAddressed = true;
outputHashMode = mode;
outputHashAlgo = "sha256";
};
hello = pkgs.runCommand "hello" (contentAddressed "nar") ''
mkdir $out
echo "Hello from a generated derivation" > $out/greeting
'';
helloDrv = pkgs.runCommand "hello.drv" (contentAddressed "text") '' # (2)!
cp ${builtins.unsafeDiscardOutputDependency hello.drvPath} $out
'';
in {
packages.x86_64-linux.default = pkgs.runCommand "dynamic-hello" (contentAddressed "nar") ''
cp -r ${builtins.outputOf helloDrv.outPath "out"} $out # (3)!
'';
};
}
- All derivations in the chain carry content-addressed outputs.
- Text-hashed outputs hold the
.drvfile, and their names end in.drv. builtins.outputOfcan point at theoutoutput of the generated derivation.
3. Allow Recursive Nix¶
Flakes without nested nix calls don't need this step. A build with recursive Nix can call nix inside the sandbox, for example nix derivation add.
- Derivations that use recursive Nix list
requiredSystemFeatures = [ "recursive-nix" ]. - The experimental feature adds the system feature
recursive-nixto the Nix store of the worker. Gradient can place such a build only on a worker withrecursive-nix. - Nested
nixcalls produce paths. Those paths land in the cache next to the outputs that refer to them.
Verify Deployment¶
Push the flake and open the task page of the new evaluation.
- Task pages show
other.x86_64-linux.hello.drvwith an IFD tag. The evaluating worker waited on that build. Then the worker read the generated.drv. - Dependencies of
packages.x86_64-linux.defaultinclude the generatedhello. - A recursive Nix build without a matching worker can wait with the reason
x86_64-linux with features recursive-nix.
Generating builds behave like Imported Derivations. Failed generating builds fail the dependent attributes with resolving a dynamic derivation input failed: ....
Limits¶
- Dynamic derivations over SSH end with the error
dynamic derivations are not supported over SSH. - Floating content-addressed derivations get no support over SSH either.