6.NUR/default.nix
Silvan Mosberger ee65c7afd6
Separate NUR nixpkgs from repos nixpkgs, avoid callPackages
This is essential to get modules in NUR to work. By taking a separate
argument for NUR's nixpkgs (for fetchgit, fetchzip and lib), we don't
need to evaluate the nixpkgs used for repos.

This also implies that you won't be able to `callPackage` NUR anymore,
and instead you'll have to use `import (builtins.fetchGit ".../NUR") {
inherit pkgs; }` instead. Doing this also prevents the evaluation of
pkgs. In case of NixOS, this pkgs depends on your whole config, which is
the source of the recursion. Evaluating this at the last possible moment
is key.

This also means that you won't be able to take package arguments in a
repo definition, you instead get just `pkgs`, also to avoid evaluation
of it.

An error will be thrown when pkgs was required for evaluation but wasn't
passed to the NUR import

The old callPackage syntax will still be supported albeit with a warning

Also repos receive a lib argument,
Using this lib instead of pkgs.lib makes it possible to define library
functions that use other library functions without depending on pkgs ->
should prevent some infinite recursion cases for NixOS module usage.
2018-07-20 04:36:18 +02:00

54 lines
1.6 KiB
Nix

{ nurpkgs ? import <nixpkgs> {} # For nixpkgs dependencies used by NUR itself
# Dependencies to call NUR repos with
, pkgs ? null }:
let
inherit (nurpkgs) fetchgit fetchzip lib;
manifest = (builtins.fromJSON (builtins.readFile ./repos.json)).repos;
lockedRevisions = (builtins.fromJSON (builtins.readFile ./repos.json.lock)).repos;
parseGitlabUrl = url:
with builtins;
let
parts = lib.splitString "/" url;
len = length parts;
in {
domain = elemAt parts 2;
owner = elemAt parts (len - 2);
repo = elemAt parts (len - 1);
};
repoSource = name: attr:
let
revision = lockedRevisions.${name};
submodules = attr.submodules or false;
in if lib.hasPrefix "https://github.com" attr.url && !submodules then
fetchzip {
url = "${attr.url}/archive/${revision.rev}.zip";
inherit (revision) sha256;
}
else if (lib.hasPrefix "https://gitlab.com" attr.url || attr.type == "gitlab") && !submodules then
let
gitlab = parseGitlabUrl attr.url;
in fetchzip {
url = "https://${gitlab.domain}/api/v4/projects/${gitlab.owner}%2F${gitlab.repo}/repository/archive.tar.gz?sha=${revision.rev}";
inherit (revision) sha256;
}
else
fetchgit {
inherit (attr) url;
inherit (revision) rev sha256;
fetchSubmodules = submodules;
};
createRepo = name: attr: import ./lib/evalRepo.nix {
inherit name pkgs lib;
inherit (attr) url;
src = repoSource name attr + "/" + (attr.file or "");
};
in {
repos = lib.mapAttrs createRepo manifest;
repo-sources = lib.mapAttrs repoSource manifest;
}