nix-helpers: 9f418ad3f37833c04ccf6d26b87a20d5e9c09844

     1: # This file has been generated by Niv.
     2: 
     3: let
     4: 
     5:   #
     6:   # The fetchers. fetch_<type> fetches specs of type <type>.
     7:   #
     8: 
     9:   fetch_file = pkgs: name: spec:
    10:     let name' = sanitizeName name + "-src";
    11:     in if spec.builtin or true then
    12:       builtins_fetchurl {
    13:         inherit (spec) url sha256;
    14:         name = name';
    15:       }
    16:     else
    17:       pkgs.fetchurl {
    18:         inherit (spec) url sha256;
    19:         name = name';
    20:       };
    21: 
    22:   fetch_tarball = pkgs: name: spec:
    23:     let name' = sanitizeName name + "-src";
    24:     in if spec.builtin or true then
    25:       builtins_fetchTarball {
    26:         name = name';
    27:         inherit (spec) url sha256;
    28:       }
    29:     else
    30:       pkgs.fetchzip {
    31:         name = name';
    32:         inherit (spec) url sha256;
    33:       };
    34: 
    35:   fetch_git = name: spec:
    36:     let
    37:       ref = if spec ? ref then
    38:         spec.ref
    39:       else if spec ? branch then
    40:         "refs/heads/${spec.branch}"
    41:       else if spec ? tag then
    42:         "refs/tags/${spec.tag}"
    43:       else
    44:         abort
    45:         "In git source '${name}': Please specify `ref`, `tag` or `branch`!";
    46:     in builtins.fetchGit {
    47:       url = spec.repo;
    48:       inherit (spec) rev;
    49:       inherit ref;
    50:     };
    51: 
    52:   fetch_local = spec: spec.path;
    53: 
    54:   fetch_builtin-tarball = name:
    55:     throw ''
    56:       [${name}] The niv type "builtin-tarball" is deprecated. You should instead use `builtin = true`.
    57:               $ niv modify ${name} -a type=tarball -a builtin=true'';
    58: 
    59:   fetch_builtin-url = name:
    60:     throw ''
    61:       [${name}] The niv type "builtin-url" will soon be deprecated. You should instead use `builtin = true`.
    62:               $ niv modify ${name} -a type=file -a builtin=true'';
    63: 
    64:   #
    65:   # Various helpers
    66:   #
    67: 
    68:   # https://github.com/NixOS/nixpkgs/pull/83241/files#diff-c6f540a4f3bfa4b0e8b6bafd4cd54e8bR695
    69:   sanitizeName = name:
    70:     (concatMapStrings (s: if builtins.isList s then "-" else s)
    71:       (builtins.split "[^[:alnum:]+._?=-]+"
    72:         ((x: builtins.elemAt (builtins.match "\\.*(.*)" x) 0) name)));
    73: 
    74:   # The set of packages used when specs are fetched using non-builtins.
    75:   mkPkgs = sources: system:
    76:     let
    77:       sourcesNixpkgs = import
    78:         (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) {
    79:           inherit system;
    80:         };
    81:       hasNixpkgsPath = builtins.any (x: x.prefix == "nixpkgs") builtins.nixPath;
    82:       hasThisAsNixpkgsPath = <nixpkgs> == ./.;
    83:     in if builtins.hasAttr "nixpkgs" sources then
    84:       sourcesNixpkgs
    85:     else if hasNixpkgsPath && !hasThisAsNixpkgsPath then
    86:       import <nixpkgs> { }
    87:     else
    88:       abort ''
    89:         Please specify either <nixpkgs> (through -I or NIX_PATH=nixpkgs=...) or
    90:         add a package called "nixpkgs" to your sources.json.
    91:       '';
    92: 
    93:   # The actual fetching function.
    94:   fetch = pkgs: name: spec:
    95: 
    96:     if !builtins.hasAttr "type" spec then
    97:       abort "ERROR: niv spec ${name} does not have a 'type' attribute"
    98:     else if spec.type == "file" then
    99:       fetch_file pkgs name spec
   100:     else if spec.type == "tarball" then
   101:       fetch_tarball pkgs name spec
   102:     else if spec.type == "git" then
   103:       fetch_git name spec
   104:     else if spec.type == "local" then
   105:       fetch_local spec
   106:     else if spec.type == "builtin-tarball" then
   107:       fetch_builtin-tarball name
   108:     else if spec.type == "builtin-url" then
   109:       fetch_builtin-url name
   110:     else
   111:       abort
   112:       "ERROR: niv spec ${name} has unknown type ${builtins.toJSON spec.type}";
   113: 
   114:   # If the environment variable NIV_OVERRIDE_${name} is set, then use
   115:   # the path directly as opposed to the fetched source.
   116:   replace = name: drv:
   117:     let
   118:       saneName = stringAsChars
   119:         (c: if isNull (builtins.match "[a-zA-Z0-9]" c) then "_" else c) name;
   120:       ersatz = builtins.getEnv "NIV_OVERRIDE_${saneName}";
   121:     in if ersatz == "" then drv else ersatz;
   122: 
   123:   # Ports of functions for older nix versions
   124: 
   125:   # a Nix version of mapAttrs if the built-in doesn't exist
   126:   mapAttrs = builtins.mapAttrs or (f: set:
   127:     with builtins;
   128:     listToAttrs (map (attr: {
   129:       name = attr;
   130:       value = f attr set.${attr};
   131:     }) (attrNames set)));
   132: 
   133:   # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295
   134:   range = first: last:
   135:     if first > last then
   136:       [ ]
   137:     else
   138:       builtins.genList (n: first + n) (last - first + 1);
   139: 
   140:   # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257
   141:   stringToCharacters = s:
   142:     map (p: builtins.substring p 1 s) (range 0 (builtins.stringLength s - 1));
   143: 
   144:   # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269
   145:   stringAsChars = f: s: concatStrings (map f (stringToCharacters s));
   146:   concatMapStrings = f: list: concatStrings (map f list);
   147:   concatStrings = builtins.concatStringsSep "";
   148: 
   149:   # https://github.com/NixOS/nixpkgs/blob/8a9f58a375c401b96da862d969f66429def1d118/lib/attrsets.nix#L331
   150:   optionalAttrs = cond: as: if cond then as else { };
   151: 
   152:   # fetchTarball version that is compatible between all the versions of Nix
   153:   builtins_fetchTarball = { url, name ? null, sha256 }@attrs:
   154:     let inherit (builtins) lessThan nixVersion fetchTarball;
   155:     in if lessThan nixVersion "1.12" then
   156:       fetchTarball
   157:       ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; }))
   158:     else
   159:       fetchTarball attrs;
   160: 
   161:   # fetchurl version that is compatible between all the versions of Nix
   162:   builtins_fetchurl = { url, name ? null, sha256 }@attrs:
   163:     let inherit (builtins) lessThan nixVersion fetchurl;
   164:     in if lessThan nixVersion "1.12" then
   165:       fetchurl
   166:       ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; }))
   167:     else
   168:       fetchurl attrs;
   169: 
   170:   # Create the final "sources" from the config
   171:   mkSources = config:
   172:     mapAttrs (name: spec:
   173:       if builtins.hasAttr "outPath" spec then
   174:         abort
   175:         "The values in sources.json should not have an 'outPath' attribute"
   176:       else
   177:         spec // { outPath = replace name (fetch config.pkgs name spec); })
   178:     config.sources;
   179: 
   180:   # The "config" used by the fetchers
   181:   mkConfig = { sourcesFile ?
   182:       if builtins.pathExists ./sources.json then ./sources.json else null
   183:     , sources ? if isNull sourcesFile then
   184:       { }
   185:     else
   186:       builtins.fromJSON (builtins.readFile sourcesFile)
   187:     , system ? builtins.currentSystem, pkgs ? mkPkgs sources system }: rec {
   188:       # The sources, i.e. the attribute set of spec name to spec
   189:       inherit sources;
   190: 
   191:       # The "pkgs" (evaluated nixpkgs) to use for e.g. non-builtin fetchers
   192:       inherit pkgs;
   193:     };
   194: 
   195: in mkSources (mkConfig { }) // {
   196:   __functor = _: settings: mkSources (mkConfig settings);
   197: }

Generated by git2html.