From 5c966856bc8b49ab34718fda760b3e4ad3fb2b96 Mon Sep 17 00:00:00 2001 From: Nicolas Mattia Date: Tue, 18 Aug 2020 10:36:59 +0200 Subject: [PATCH] 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. --- nix/sources.nix | 38 +++++++++++++++++++++++++------------- src/Niv/Sources.hs | 4 ++++ 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/nix/sources.nix b/nix/sources.nix index b64b8f8..d0b91b0 100644 --- a/nix/sources.nix +++ b/nix/sources.nix @@ -6,17 +6,18 @@ let # The fetchers. fetch_ fetches specs of 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; diff --git a/src/Niv/Sources.hs b/src/Niv/Sources.hs index b288f5a..4efd645 100644 --- a/src/Niv/Sources.hs +++ b/src/Niv/Sources.hs @@ -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