YOUR JOURNEY
16 / 18
03THINKING IN FLAKES 7 MIN READ
LESSON 16 / BUILD & RUN OUTPUTS

Name the output.
Let Nix find it.

A flake can expose something you can build and something you can run.

Output conventions connect a project’s Nix values to commands. Here is a complete, small executable package.

Explore the lesson
~$ Hands-on examples · Nix with flakes enabled.
THE NIX MODEL01 / 03
Drag to rotate

packages.<system>.default exposes a default package.

Default package exposed
16.1A RUNNABLE PACKAGE

Make the output useful.

This flake creates a tiny greeting program. writeShellApplication supplies a shell-script package, and runtimeInputs puts the declared executable dependency on the script’s PATH.

The default package has a main program, so nix run can use it without a separate apps output. Larger projects can expose multiple named packages and app definitions.

flake.nix
{
  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  outputs = { nixpkgs, ... }: let
    system = "x86_64-linux";
    pkgs = nixpkgs.legacyPackages.${system};
  in {
    packages.${system}.default = pkgs.writeShellApplication {
      name = "greet";
      runtimeInputs = [ pkgs.hello ];
      text = ''
        hello --greeting="Hello from a flake!"
      '';
    };
  };
}
Next section: Build, inspect, then execute.
16.2FOLLOW THE OUTPUT

Build, inspect, then execute.

Use this in its own project, or add the packages output to the preceding dev-shell flake. Add flake.nix to Git if the directory is a repository.

The dot means the current flake. The # separates a flake reference from an output name. A default output lets you omit the explicit name.

terminal
git add flake.nix
nix flake show
nix build .
./result/bin/greet
nix run .
# Expected: Hello from a flake!
TAKE THIS WITH YOU

nix build obtains an output; nix run executes a discoverable app or package program.

Go a little deepernix run reference
UP NEXT

NixOS meets flakes

Expose a NixOS configuration as a named flake output.

Next lesson