123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- let
-
-
-
- fetch_file = pkgs: name: spec:
- let
- name' = sanitizeName name + "-src";
- in
- if spec.builtin or true then
- builtins_fetchurl { inherit (spec) url sha256; name = name'; }
- else
- pkgs.fetchurl { inherit (spec) url sha256; name = name'; };
- fetch_tarball = pkgs: name: spec:
- let
- name' = sanitizeName name + "-src";
- in
- if spec.builtin or true then
- builtins_fetchTarball { name = name'; inherit (spec) url sha256; }
- else
- pkgs.fetchzip { name = name'; inherit (spec) url sha256; };
- fetch_git = name: spec:
- let
- ref =
- spec.ref or (
- if spec ? branch then "refs/heads/${spec.branch}" else
- if spec ? tag then "refs/tags/${spec.tag}" else
- abort "In git source '${name}': Please specify `ref`, `tag` or `branch`!"
- );
- submodules = spec.submodules or false;
- submoduleArg =
- let
- nixSupportsSubmodules = builtins.compareVersions builtins.nixVersion "2.4" >= 0;
- emptyArgWithWarning =
- if submodules
- then
- builtins.trace
- (
- "The niv input \"${name}\" uses submodules "
- + "but your nix's (${builtins.nixVersion}) builtins.fetchGit "
- + "does not support them"
- )
- { }
- else { };
- in
- if nixSupportsSubmodules
- then { inherit submodules; }
- else emptyArgWithWarning;
- in
- builtins.fetchGit
- ({ url = spec.repo; inherit (spec) rev; inherit ref; } // submoduleArg);
- fetch_local = spec: spec.path;
- fetch_builtin-tarball = name: throw
- ''[${name}] The niv type "builtin-tarball" is deprecated. You should instead use `builtin = true`.
- $ niv modify ${name} -a type=tarball -a builtin=true'';
- fetch_builtin-url = name: throw
- ''[${name}] The niv type "builtin-url" will soon be deprecated. You should instead use `builtin = true`.
- $ niv modify ${name} -a type=file -a builtin=true'';
-
-
-
-
- sanitizeName = name:
- (
- concatMapStrings (s: if builtins.isList s then "-" else s)
- (
- builtins.split "[^[:alnum:]+._?=-]+"
- ((x: builtins.elemAt (builtins.match "\\.*(.*)" x) 0) name)
- )
- );
-
- mkPkgs = sources: system:
- let
- sourcesNixpkgs =
- import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) { inherit system; };
- hasNixpkgsPath = builtins.any (x: x.prefix == "nixpkgs") builtins.nixPath;
- hasThisAsNixpkgsPath = <nixpkgs> == ./.;
- in
- if builtins.hasAttr "nixpkgs" sources
- then sourcesNixpkgs
- else if hasNixpkgsPath && ! hasThisAsNixpkgsPath then
- import <nixpkgs> { }
- else
- abort
- ''
- Please specify either <nixpkgs> (through -I or NIX_PATH=nixpkgs=...) or
- add a package called "nixpkgs" to your sources.json.
- '';
-
- 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 pkgs name spec
- else if spec.type == "tarball" then fetch_tarball pkgs name spec
- else if spec.type == "git" then fetch_git name spec
- else if spec.type == "local" then fetch_local spec
- else if spec.type == "builtin-tarball" then fetch_builtin-tarball name
- else if spec.type == "builtin-url" then fetch_builtin-url name
- else
- abort "ERROR: niv spec ${name} has unknown type ${builtins.toJSON spec.type}";
-
-
- replace = name: drv:
- let
- saneName = stringAsChars (c: if (builtins.match "[a-zA-Z0-9]" c) == null then "_" else c) name;
- ersatz = builtins.getEnv "NIV_OVERRIDE_${saneName}";
- in
- if ersatz == "" then drv else
-
-
- if builtins.substring 0 1 ersatz == "/" then /. + ersatz else /. + builtins.getEnv "PWD" + "/${ersatz}";
-
-
- mapAttrs = builtins.mapAttrs or (
- f: set: with builtins;
- listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set))
- );
-
- range = first: last: if first > last then [ ] else builtins.genList (n: first + n) (last - first + 1);
-
- stringToCharacters = s: map (p: builtins.substring p 1 s) (range 0 (builtins.stringLength s - 1));
-
- stringAsChars = f: s: concatStrings (map f (stringToCharacters s));
- concatMapStrings = f: list: concatStrings (map f list);
- concatStrings = builtins.concatStringsSep "";
-
- optionalAttrs = cond: as: if cond then as else { };
-
- builtins_fetchTarball = { url, name ? null, sha256 }@attrs:
- let
- inherit (builtins) lessThan nixVersion fetchTarball;
- in
- if lessThan nixVersion "1.12" then
- fetchTarball ({ inherit url; } // (optionalAttrs (name != null) { inherit name; }))
- else
- fetchTarball attrs;
-
- builtins_fetchurl = { url, name ? null, sha256 }@attrs:
- let
- inherit (builtins) lessThan nixVersion fetchurl;
- in
- if lessThan nixVersion "1.12" then
- fetchurl ({ inherit url; } // (optionalAttrs (name != null) { inherit name; }))
- else
- fetchurl attrs;
-
- 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 = replace name (fetch config.pkgs name spec); }
- )
- config.sources;
-
- mkConfig =
- { sourcesFile ? if builtins.pathExists ./sources.json then ./sources.json else null
- , sources ? if sourcesFile == null then { } else builtins.fromJSON (builtins.readFile sourcesFile)
- , system ? builtins.currentSystem
- , pkgs ? mkPkgs sources system
- }: rec {
-
- inherit sources;
-
- inherit pkgs;
- };
- in
- mkSources (mkConfig { }) // { __functor = _: settings: mkSources (mkConfig settings); }
|