2021-02-14 02:38:20 +00:00
# Packages
2021-04-19 02:26:27 +00:00
Similar to [modules ](./modules.md ), the pkgs directory mirrors the upstream
2021-02-14 02:38:20 +00:00
[nixpkgs/pkgs][pkgs], and for the same reason; if you ever want to upstream
y our package, it's as simple as dropping it into the nixpkgs/pkgs directory.
The only minor difference is that, instead of adding the `callPackage` call to
`all-packages.nix` , you just add it the the _default.nix_ in this directory,
which is defined as a simple overlay.
2021-06-03 19:25:06 +00:00
All the packages are exported via `packages.<system>.<pkg-name>` , for all
2021-02-14 02:38:20 +00:00
the supported systems listed in the package's `meta.platforms` attribute.
And, as usual, every package in the overlay is also available to any NixOS
2021-04-19 02:26:27 +00:00
[host ](../concepts/hosts.md ).
2021-02-14 02:38:20 +00:00
2021-09-02 09:37:21 +00:00
Another convenient difference is that it is possible to use
[nvfetcher ](https://github.com/berberman/nvfetcher ) to keep packages up to
date.
This is best understood by the simple example below.
2021-02-14 02:38:20 +00:00
## Example
2021-09-02 09:37:21 +00:00
It is possible to specify sources separately to keep them up to date semi
automatically.
The basic rules are specified in pkgs/sources.toml:
```toml
# nvfetcher.toml
[libinih]
src.github = "benhoyt/inih"
fetch.github = "benhoyt/inih"
```
After changes to this file as well as to update the packages specified in there run
nvfetcher (for more details see [nvfetcher ](https://github.com/berberman/nvfetcher )).
The pkgs overlay is managed in
pkgs/default.nix:
```nix
final: prev: {
# keep sources first, this makes sources available to the pkgs
sources = prev.callPackage (import ./_sources/generated.nix) { };
# then, call packages with `final.callPackage`
libinih = prev.callPackage ./development/libraries/libinih { };
}
```
Lastly the example package is in
2021-02-14 02:38:20 +00:00
pkgs/development/libraries/libinih/default.nix:
```nix
2021-09-02 09:37:21 +00:00
{ stdenv, meson, ninja, lib, sources, ... }:
2021-02-14 02:38:20 +00:00
stdenv.mkDerivation {
pname = "libinih";
2021-09-02 09:37:21 +00:00
# version will resolve to the latest available on gitub
inherit (sources.libinih) version src;
2021-02-14 02:38:20 +00:00
2021-03-14 07:10:51 +00:00
buildInputs = [ meson ninja ];
2021-02-14 02:38:20 +00:00
2021-03-14 07:10:51 +00:00
# ...
2021-02-14 02:38:20 +00:00
}
```
2021-09-02 09:37:21 +00:00
## Migration from flake based approach
Previous to nvfetcher it was possible to manage sources via a pkgs/flake.nix, the main changes from there are that sources where in the attribute "srcs" (now "sources") and the contents of the sources where slightly different.
In order to switch to the new system, rewrite pkgs/flake.nix to a pkgs/sources.toml file using the documentation of nvfetcher,
add the line that calls the sources at the beginning of pkgs/default.nix, and
accomodate the small changes in the packages as can be seen from the example.
The example package looked like:
2021-02-14 02:38:20 +00:00
2021-02-23 02:49:31 +00:00
pkgs/flake.nix:
```nix
{
description = "Package sources";
inputs = {
libinih.url = "github:benhoyt/inih/r53";
libinih.flake = false;
};
}
```
2021-09-02 09:37:21 +00:00
pkgs/default.nix:
```nix
final: prev: {
# then, call packages with `final.callPackage`
libinih = prev.callPackage ./development/libraries/libinih { };
}
```
pkgs/development/libraries/libinih/default.nix:
```nix
{ stdenv, meson, ninja, lib, srcs, ... }:
let inherit (srcs) libinih; in
stdenv.mkDerivation {
pname = "libinih";
# version will resolve to 53, as specified in the flake.nix file
inherit (libinih) version;
src = libinih;
buildInputs = [ meson ninja ];
# ...
}
```
2021-02-14 02:38:20 +00:00
[pkgs]: https://github.com/NixOS/nixpkgs/tree/master/pkgs