1
1
mirror of https://github.com/nmattia/niv.git synced 2024-09-18 19:07:19 +03:00

Sanitize source name

The `type` sources didn't get their name sanitized. This also fixes a
bug where builtins_fetchTarball would fail if because no name was given.
This commit is contained in:
Nicolas Mattia 2020-08-18 10:36:59 +02:00
parent 13c88bfcc3
commit 5c966856bc
2 changed files with 29 additions and 13 deletions

View File

@ -6,17 +6,18 @@ let
# The fetchers. fetch_<type> fetches specs of type <type>.
#
fetch_file = pkgs: spec:
if spec.builtin or true then
builtins_fetchurl { inherit (spec) url sha256; }
else
pkgs.fetchurl { inherit (spec) url sha256; };
fetch_file = pkgs: name: spec:
let
name' = sanitizeName name;
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
ok = str: ! builtins.isNull (builtins.match "[a-zA-Z0-9+-._?=]" str);
# sanitize the name, though nix will still fail if name starts with period
name' = stringAsChars (x: if ! ok x then "-" else x) "${name}-src";
name' = sanitizeName name;
in
if spec.builtin or true then
builtins_fetchTarball { name = name'; inherit (spec) url sha256; }
@ -40,6 +41,14 @@ let
# Various helpers
#
# sanitize the name, though nix will still fail if name starts with period
sanitizeName = name:
let
ok = str: ! builtins.isNull (builtins.match "[a-zA-Z0-9+-._?=]" str);
in
stringAsChars (x: if ! ok x then "-" else x) "${name}-src";
# The set of packages used when specs are fetched using non-builtins.
mkPkgs = sources:
let
@ -64,7 +73,7 @@ let
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 spec
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 spec
else if spec.type == "local" then fetch_local spec
@ -100,23 +109,26 @@ let
stringAsChars = f: s: concatStrings (map f (stringToCharacters s));
concatStrings = builtins.concatStringsSep "";
# https://github.com/NixOS/nixpkgs/blob/8a9f58a375c401b96da862d969f66429def1d118/lib/attrsets.nix#L331
optionalAttrs = cond: as: if cond then as else {};
# fetchTarball version that is compatible between all the versions of Nix
builtins_fetchTarball = { url, name, sha256 }@attrs:
builtins_fetchTarball = { url, name ? null, sha256 }@attrs:
let
inherit (builtins) lessThan nixVersion fetchTarball;
in
if lessThan nixVersion "1.12" then
fetchTarball { inherit name url; }
fetchTarball ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; }))
else
fetchTarball attrs;
# fetchurl version that is compatible between all the versions of Nix
builtins_fetchurl = { url, sha256 }@attrs:
builtins_fetchurl = { url, name ? null, sha256 }@attrs:
let
inherit (builtins) lessThan nixVersion fetchurl;
in
if lessThan nixVersion "1.12" then
fetchurl { inherit url; }
fetchurl ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; }))
else
fetchurl attrs;

View File

@ -164,6 +164,8 @@ data SourcesNixVersion
V19
| -- can be imported when there's no sources.json
V20
| -- Use the source name in fetchurl
V21
deriving stock (Bounded, Enum, Eq)
-- | A user friendly version
@ -189,6 +191,7 @@ sourcesVersionToText = \case
V18 -> "18"
V19 -> "19"
V20 -> "20"
V21 -> "21"
latestVersionMD5 :: T.Text
latestVersionMD5 = sourcesVersionToMD5 maxBound
@ -221,6 +224,7 @@ sourcesVersionToMD5 = \case
V18 -> "bc5e6aefcaa6f9e0b2155ca4f44e5a33"
V19 -> "543621698065cfc6a4a7985af76df718"
V20 -> "ab4263aa63ccf44b4e1510149ce14eff"
V21 -> "0b888c92e69629dd954f485bfb4a58ed"
-- | The MD5 sum of ./nix/sources.nix
sourcesNixMD5 :: IO T.Text