In flake, it is achieved by something like this (syntax may wrong but you get the idea):
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
};
outputs =
{ nixpkgs }:
let
pkgs = nixpkgs.legacyPackages.x86_64-linux;
in
{
packages = {
a = pkgs.callPackage ./a { };
b = pkgs.callPackage ./b { };
c = pkgs.callPackage ./c { };
};
};
}
How to do something it in standard nix? I cannot find it on nix wiki.
You can use a plain attrset in a `default.nix` file:
let
nixpkgs = builtins.fetchTarball {
url = "https://github.com/NixOS/nixpkgs/archive/61b7c44c4073f0b827768aff0049561b5110ea5a.tar.gz";
sha256 = "";
};
pkgs = import nixpkgs { };
in
{
a = pkgs.callPackage ./a { };
b = pkgs.callPackage ./b { };
c = pkgs.callPackage ./c { };
}
Build with `nix-build -A a`. Flakes basically only provide the automatic input pinning updates in this case. There are other tools like npins or nixtamal, but its also valid to just manually update the revision and hash.
Make sure to get the revision from Commits · NixOS/nixpkgs · GitHub so that you have cached derivations.
The above is great, but instead of writing a ./default.nix that returns an attrset, consider writing one that returns a function whose argument is an attrset with defaults defined. When you nix-build such a file, all the arguments get set to their defaults, so it behaves transparently in this respect exactly as if you just defined an attrset of packages instead of a function. The advantage of it being a function is that then callers have an easy way to override the inputs (analogous to follows). This behavior is documented in man nix-build around the --arg option.
Concretely:
let
nixpkgs = builtins.fetchTarball {
url = "https://github.com/NixOS/nixpkgs/archive/61b7c44c4073f0b827768aff0049561b5110ea5a.tar.gz";
sha256 = "";
};
in
{
pkgs ? import nixpkgs {};
}:
{
a = pkgs.callPackage ./a { };
b = pkgs.callPackage ./b { };
c = pkgs.callPackage ./c { };
}
Also consider A nix shell with a nix package in a git repo - #5 by tejing for a great pattern which combines ability to nix-build, import and callPackage the same file for each of your ./a/default.nix, ./b/default.nix, etc.
6 Likes