Skip to content

Use Dynamic Derivations

A flake with derivations generated at build time, built and cached by Gradient.

Requirements:

1. Enable the Nix Features

The worker module came with ca-derivations already on. Enable dynamic-derivations on every worker host.

# configuration.nix
nix.settings.experimental-features = [ "dynamic-derivations" ]; # (1)!
  1. Worker module features can merge with this list in NixOS.

  2. Worker hosts keep their features in nix.conf. Evaluators read that file.

  3. The nix-daemon can 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)!
    '';
  };
}
  1. All derivations in the chain carry content-addressed outputs.
  2. Text-hashed outputs hold the .drv file, and their names end in .drv.
  3. builtins.outputOf can point at the out output 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.

# configuration.nix
nix.settings.experimental-features = [ "recursive-nix" ];
  • Derivations that use recursive Nix list requiredSystemFeatures = [ "recursive-nix" ].
  • The experimental feature adds the system feature recursive-nix to the Nix store of the worker. Gradient can place such a build only on a worker with recursive-nix.
  • Nested nix calls 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.drv with an IFD tag. The evaluating worker waited on that build. Then the worker read the generated .drv.
  • Dependencies of packages.x86_64-linux.default include the generated hello.
  • 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.

Next steps