1
1
mirror of https://github.com/nmattia/niv.git synced 2024-11-29 00:42:04 +03:00

Allow specifying custom sources.json

This turns `sources.nix` into a functor that acceps an argument:

``` nix
{ sourcesJson = <path/to/sources.json> ; }
```

The `sourcesJson` will be used instead of `./sources.json`.
This commit is contained in:
Nicolas Mattia 2019-12-08 12:15:40 +01:00
parent e14fbaab92
commit 3e1d44bbfa
2 changed files with 35 additions and 18 deletions

View File

@ -6,13 +6,13 @@ let
# The fetchers. fetch_<type> fetches specs of type <type>.
#
fetch_file = spec:
fetch_file = pkgs: spec:
if spec.builtin or true then
builtins_fetchurl { inherit (spec) url sha256; }
else
pkgs.fetchurl { inherit (spec) url sha256; };
fetch_tarball = spec:
fetch_tarball = pkgs: spec:
if spec.builtin or true then
builtins_fetchTarball { inherit (spec) url sha256; }
else
@ -47,23 +47,23 @@ let
# The sources to fetch.
#
sources = builtins.fromJSON (builtins.readFile ./sources.json);
sourcesDefault = builtins.fromJSON (builtins.readFile ./sources.json);
#
# Various helpers
#
# The set of packages used when specs are fetched using non-builtins.
pkgs =
mkPkgs = sources:
if hasNixpkgsPath
then
if hasThisAsNixpkgsPath
then import (builtins_fetchTarball { inherit (sources_nixpkgs) url sha256; }) {}
then import (builtins_fetchTarball { inherit (mkNixpkgs sources) url sha256; }) {}
else import <nixpkgs> {}
else
import (builtins_fetchTarball { inherit (sources_nixpkgs) url sha256; }) {};
import (builtins_fetchTarball { inherit (mkNixpkgs sources) url sha256; }) {};
sources_nixpkgs =
mkNixpkgs = sources:
if builtins.hasAttr "nixpkgs" sources
then sources.nixpkgs
else abort
@ -77,12 +77,12 @@ let
(builtins.tryEval <nixpkgs>).success && <nixpkgs> == ./.;
# The actual fetching function.
fetch = name: spec:
fetch = pkgs: name: spec:
if ! builtins.hasAttr "type" spec then
abort "ERROR: niv spec ${name} does not have a 'type' attribute"
else if spec.type == "file" then fetch_file spec
else if spec.type == "tarball" then fetch_tarball spec
else if spec.type == "file" then fetch_file pkgs spec
else if spec.type == "tarball" then fetch_tarball pkgs spec
else if spec.type == "git" then fetch_git spec
else if spec.type == "builtin-tarball" then fetch_builtin-tarball spec
else if spec.type == "builtin-url" then fetch_builtin-url spec
@ -117,12 +117,26 @@ let
else
fetchurl attrs;
# Create the final "sources" from the config
mkSources = config:
mapAttrs (
name: spec:
if builtins.hasAttr "outPath" spec
then abort
"The values in sources.json should not have an 'outPath' attribute"
else
spec // { outPath = fetch config.pkgs name spec; }
) config.sources;
# The "config" used by the fetchers
mkConfig =
{ sourcesFile ? ./sources.json
}: rec {
# The sources, i.e. the attribute set of spec name to spec
sources = builtins.fromJSON (builtins.readFile sourcesFile);
# The "pkgs" (evaluated nixpkgs) to use for e.g. non-builtin fetchers
pkgs = mkPkgs sources;
};
in
mapAttrs (
name: spec:
if builtins.hasAttr "outPath" spec
then abort
"The values in sources.json should not have an 'outPath' attribute"
else
spec // { outPath = fetch name spec; }
) sources
mkSources (mkConfig {}) //
{ __functor = _: settings: mkSources (mkConfig settings); }

View File

@ -136,6 +136,7 @@ data SourcesNixVersion
| V10
| V11
| V12
| V13
deriving stock (Bounded, Enum, Eq)
-- | A user friendly version
@ -153,6 +154,7 @@ sourcesVersionToText = \case
V10 -> "10"
V11 -> "11"
V12 -> "12"
V13 -> "13"
latestVersionMD5 :: T.Text
latestVersionMD5 = sourcesVersionToMD5 maxBound
@ -177,6 +179,7 @@ sourcesVersionToMD5 = \case
V10 -> "d8625c0a03dd935e1c79f46407faa8d3"
V11 -> "8a95b7d93b16f7c7515d98f49b0ec741"
V12 -> "2f9629ad9a8f181ed71d2a59b454970c"
V13 -> "4c745eb8b57ac0fbb664ed4e24a6aa36"
-- | The MD5 sum of ./nix/sources.nix
sourcesNixMD5 :: IO T.Text