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.
{
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!"
'';
};
};
}
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.
git add flake.nix
nix flake show
nix build .
./result/bin/greet
nix run .
# Expected: Hello from a flake!
nix build obtains an output; nix run executes a discoverable app or package program.