Skip to content

Build a Repository Without flake.nix

Nix repositories that have default.nix or shell.nix build on Gradient without a hand-written flake.nix. Lookups such as <nixpkgs> come from the project's Nix channels. Evaluations fix the channel version.

Requirements:

  • A project with a task, see First Project.
  • Workers with the same Gradient version as the server. Older workers do not take evaluations of a project with Nix channels turned on.

1. Turn On Nix Channels

Open the project's Settings -> General. Check Enable Nix channels and save. The Nix channels tab is next to General.

gradient project select acme
gradient project edit --enable-nix-channels true
services.gradient.state.projects.acme.enable_nix_channels = true;

The saved channels remain when the setting is off. Repositories without flake.nix fail evaluation again. The trigger for Nix channels can no longer fire.

2. Add Channels

A name and a URL make up each channel. The name is the reference name in Nix. The target of the nixpkgs channel is <nixpkgs> and every path under it, such as <nixpkgs/lib>.

Open Settings -> Nix channels -> Add channel. Enter the name nixpkgs and the URL https://channels.nixos.org/nixos-unstable.

gradient project channel add nixpkgs https://channels.nixos.org/nixos-unstable
services.gradient.state.projects.acme.nix_channels = {
  nixpkgs = "https://channels.nixos.org/nixos-unstable";
};
  • Name: Letters, digits, _, -. The first character is a letter or _. The name self stays reserved for the flake itself.
  • URL: http or https without credentials. URLs with a tarball extension (.tar.xz, .tar.gz, .tar.bz2, .tar.zst, .tgz, .zip) remain unchanged. Other URLs get /nixexprs.tar.xz appended.
  • Private Hosts: Hosts on a private network need an entry in outbound.allowedNonPublicNetworkTargets. See Configuration.
  • Credentials: Gradient does not support channels that need a login.

3. Point the Task at legacyPackages

Some commits have no flake.nix. Generated flake.nix and flake.lock files take their place. Project channels turn into locked tarball inputs. Task wildcards then select outputs from the generated flake.

File Flake output Wildcard
default.nix legacyPackages.<system> legacyPackages.x86_64-linux.*
shell.nix devShells.<system>.default devShells.x86_64-linux.default
  • Files that evaluate to a function get an empty attribute set { } as argument, like nix-build. Functions with required arguments can fail with a Nix error.
  • Single derivations turn into default. Lists turn into attribute sets whose names are the positions 0, 1, ...
  • All outputs exist in x86_64-linux, aarch64-linux, x86_64-darwin, aarch64-darwin. Outputs read their system from builtins.currentSystem.
  • <name> with no project channels can fail with the usual error from a Nix lookup.
  • Evaluation is pure and restricted. Three things don't work, namely builtins.fetchTarball without a hash, builtins.currentTime and environment variables.

The wildcard field of Connect repository shows the legacyPackages hint for projects with Nix channels.

gradient task create --name classic --display-name Classic --description "" \
  --repository https://git.example.com/acme/classic.git \
  --wildcard 'legacyPackages.x86_64-linux.*'
services.gradient.state.tasks.classic = {
  project = "acme";
  repository = "https://git.example.com/acme/classic.git";
  wildcard = "legacyPackages.x86_64-linux.*";
  created_by = "admin";
};

4. Follow Channel Updates

A trigger for a Nix channel can start an evaluation of an unchanged commit on a newer tarball from the project channel. Builds of unchanged packages come from the cache. See Nix Channel for the rules.

Pick the trigger type Nix channel on the task's Triggers page. Connect repository offers the same trigger as a checkbox.

Add a trigger for a Nix channel to an existing task.

gradient task select classic
gradient task trigger create --config '{"type":"nix_channel_polling","interval_secs":3600}'
services.gradient.state.tasks.classic.triggers = [
  { type = "polling"; config.interval_secs = 300; }
  { type = "nix_channel_polling"; config.interval_secs = 3600; }
];

Pins

  • Evaluations record the exact tarball for every channel, with the locked URL, NAR hash and last modified time.
  • Retries of an evaluation reuse that pin. New evaluations re-resolve every channel.
  • Channel servers such as channels.nixos.org respond with redirects and immutable link headers. Locks use that immutable URL in place of the moving channel URL.

Limits

  • Exec jobs check out the real commit without generated files. A job can't use nix run .#app in these repositories.
  • Pull requests that update flake.lock need a real flake.nix in the repository.
  • gradient eval and gradient build --run need flake.nix. gradient build and gradient download in a directory without flake.nix turn bare attribute names into legacyPackages.<system>.<name>.
  • Overrides of a flake input by channel name replace that channel for that task. Builds that use gradient build --override-input nixpkgs github:NixOS/nixpkgs use that flake reference instead of the channel.

Verify Deployment

Evaluation pages show a banner Evaluated through a generated flake.nix. The banner rows list each channel with the lookup name, locked URL, NAR hash and date. Wildcards with no match fail the evaluation and add a hint to legacyPackages.<system>.

Next Steps