Share code example
This is an example for NixOS/nixpkgs#217679
The goal is to reduce duplication between Nixpkgs and Nix files in upstream projects (like this project).
To do that, the idea is to commit the shared nix expressions to Nixpkgs. Following some convetions, we can reuse the nix expressions in the upstream project.
For a full derivation example, see https://github.com/jlesquembre/nixpkgs/blob/share-nixpkgs/pkgs/applications/misc/simple-pkg/default.nix
-
(upstream) Define a package (in this example, in the
flake.nix
) like this:packages.default = pkgs.callPackage "${nixpkgs}/pkgs/applications/misc/simple-pkg/default.nix" { # Use this as src, overriding the value on nixpkgs projectInfo = { src = ./.; version = "DEV"; }; };
-
(Nixpkgs) Our expression is defined like this:
{ # ... nixpkgsUpdater # Optional , srcFromJSON # projectInfo is overloaded, it can be: # - Path to an "info.json" file (That's the case in nixpkgs) # - Attribute set with values to use in mkDerivation , projectInfo ? srcFromJSON ./info.json }: stdenv.mkDerivation { pname = "simple"; inherit (projectInfo) src version; # Function to be executed by r-ryantm bot # nixpkgsUpdater is optional passthru.updateScript = nixpkgsUpdater { fetcher = "fetchFromGitHub"; fetcherArgs = { owner = "github-username"; repo = "simple-flake"; }; }; }
-
(Nixpkgs) Notice that we have a
info.json
file in the same directory that thedefault.nix
file:{ "fetcher": "fetchFromGitHub", "fetcherArgs": { "hash": "sha256-x9BrU5hj6HsnRudt7a7qEJOTX2WU1UdAxiPK40NLvew=", "owner": "jlesquembre", "repo": "simple-flake", "rev": "v1.0" }, "version": "1.0" }
info.json
has to provide 2 fields,fetcher
andfetcherArgs
.fetcher
is one of the fetchers defined in Nixpkgs manual: Fetcher.fetcherArgs
are the arguments to that function. Any other fields would be passed tomkDerivation
, in theprojectInfo
attribute set.We can create
info.json
manually or with thenixpkgsUpdater
helper function:nix build /path/to/local/nixpkgs#simple-pkg.updateScript; ./result
Update shared nix code
In our repository, nixpkgs is an input:
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
};
To do changes, we can point nixpkgs to our local copy:
inputs = {
nixpkgs.url = "/path/to/my/local/nixpkgs";
};