From 3e1d44bbfae85cba46a5654412d7dc6196e259e8 Mon Sep 17 00:00:00 2001 From: Nicolas Mattia Date: Sun, 8 Dec 2019 12:15:40 +0100 Subject: [PATCH] Allow specifying custom sources.json This turns `sources.nix` into a functor that acceps an argument: ``` nix { sourcesJson = ; } ``` The `sourcesJson` will be used instead of `./sources.json`. --- nix/sources.nix | 50 +++++++++++++++++++++++++++++----------------- src/Niv/Sources.hs | 3 +++ 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/nix/sources.nix b/nix/sources.nix index 4c0351c..e86b9e3 100644 --- a/nix/sources.nix +++ b/nix/sources.nix @@ -6,13 +6,13 @@ let # The fetchers. fetch_ fetches specs of 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 {} 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 ).success && == ./.; # 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); } diff --git a/src/Niv/Sources.hs b/src/Niv/Sources.hs index bfb1fdf..af6b0ee 100644 --- a/src/Niv/Sources.hs +++ b/src/Niv/Sources.hs @@ -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