Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,8 @@ let
pathHasContext
canCleanSource
pathIsGitRepo
revOrTag
repoRevToName
;
inherit (self.modules)
evalModules
Expand Down
102 changes: 101 additions & 1 deletion lib/sources.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Tested in lib/tests/sources.sh
let
inherit (builtins)
inherit (lib.strings)
match
split
storeDir
Expand Down Expand Up @@ -403,6 +403,101 @@ let
};
};

# urlToName : (URL | Path | String) -> String
#
# Transform a URL (or path, or string) into a clean package name.
urlToName =
url:
let
inherit (lib.strings) stringLength;
base = baseNameOf (lib.removeSuffix "/" (lib.last (lib.splitString ":" (toString url))));
# chop away one git or archive-related extension
removeExt =
name:
let
matchExt = match "(.*)\\.(git|tar|zip|gz|tgz|bz|tbz|bz2|tbz2|lzma|txz|xz|zstd)$" name;
in
if matchExt != null then lib.head matchExt else name;
# apply function f to string x while the result shrinks
shrink =
f: x:
let
v = f x;
in
if stringLength v < stringLength x then shrink f v else x;
in
shrink removeExt base;

# shortRev : (String | Integer) -> String
#
# Given a package revision (like "refs/tags/v12.0"), produce a short revision ("12.0").
shortRev =
rev:
let
baseRev = baseNameOf (toString rev);
matchHash = match "[a-f0-9]+" baseRev;
matchVer = match "([A-Za-z]+[-_. ]?)*(v)?([0-9.]+.*)" baseRev;
in
if matchHash != null then
builtins.substring 0 7 baseRev
else if matchVer != null then
lib.last matchVer
else
baseRev;

# revOrTag : String -> String -> String
#
# Turn git `rev` and `tag` pair into a revision usable in `repoRevToName*`.
revOrTag =
rev: tag:
if tag != null then
tag
else if rev != null then
rev
else
"HEAD";

# repoRevToNameFull : (URL | Path | String) -> (String | Integer | null) -> (String | null) -> String
#
# See `repoRevToName` below.
repoRevToNameFull =
repo_: rev_: suffix_:
let
repo = urlToName repo_;
rev = if rev_ != null then "-${shortRev rev_}" else "";
suffix = if suffix_ != null then "-${suffix_}" else "";
in
"${repo}${rev}${suffix}-source";

# repoRevToName : String -> (URL | Path | String) -> (String | Integer | null) -> String -> String
#
# Produce derivation.name attribute for a given repository URL/path/name and (optionally) its revision/version tag.
#
# This is used by fetch(zip|git|FromGitHub|hg|svn|etc) to generate discoverable
# /nix/store paths.
#
# This uses a different implementation depending on the `pretty` argument:
# "source" -> name everything as "source"
# "versioned" -> name everything as "${repo}-${rev}-source"
# "full" -> name everything as "${repo}-${rev}-${fetcher}-source"
repoRevToName =
kind:
# match on `kind` first to minimize the thunk
if kind == "source" then
(
repo: rev: suffix:
"source"
)
else if kind == "versioned" then
(
repo: rev: suffix:
repoRevToNameFull repo rev null
)
else if kind == "full" then
repoRevToNameFull
else
throw "repoRevToName: invalid kind";

in
{

Expand Down Expand Up @@ -431,6 +526,11 @@ in
pathHasContext
canCleanSource

urlToName
shortRev
revOrTag
repoRevToName

sourceByRegex
sourceFilesBySuffices

Expand Down
8 changes: 6 additions & 2 deletions pkgs/build-support/fetchbitbucket/default.nix
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
{ fetchzip, lib }:
{
lib,
repoRevToNameMaybe,
fetchzip,
}:

lib.makeOverridable (
{
owner,
repo,
rev,
name ? "source",
name ? repoRevToNameMaybe repo rev "bitbucket",
... # For hash agility
}@args:
fetchzip (
Expand Down
20 changes: 8 additions & 12 deletions pkgs/build-support/fetchgit/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,26 @@
git-lfs,
cacert,
}:

let
urlToName =
url: rev:
let
inherit (lib) removeSuffix splitString last;
base = last (splitString ":" (baseNameOf (removeSuffix "/" url)));

matched = builtins.match "(.*)\\.git" base;

short = builtins.substring 0 7 rev;

appendShort = lib.optionalString ((builtins.match "[a-f0-9]*" rev) != null) "-${short}";
shortRev = lib.sources.shortRev rev;
appendShort = lib.optionalString ((builtins.match "[a-f0-9]*" rev) != null) "-${shortRev}";
in
"${if matched == null then base else builtins.head matched}${appendShort}";
"${lib.sources.urlToName url}${appendShort}";
in

lib.makeOverridable (
lib.fetchers.withNormalizedHash { } (
# NOTE Please document parameter additions or changes in
# doc/build-helpers/fetchers.chapter.md
# ../../../doc/build-helpers/fetchers.chapter.md
{
url,
tag ? null,
rev ? null,
name ? urlToName url (lib.revOrTag rev tag),
leaveDotGit ? deepClone || fetchTags,
outputHash ? lib.fakeHash,
outputHashAlgo ? null,
Expand All @@ -36,7 +33,6 @@ lib.makeOverridable (
branchName ? null,
sparseCheckout ? [ ],
nonConeMode ? false,
name ? null,
nativeBuildInputs ? [ ],
# Shell code executed before the file has been fetched. This, in
# particular, can do things like set NIX_PREFETCH_GIT_CHECKOUT_HOOK to
Expand Down Expand Up @@ -108,7 +104,7 @@ lib.makeOverridable (
"Please provide directories/patterns for sparse checkout as a list of strings. Passing a (multi-line) string is not supported any more."
else
stdenvNoCC.mkDerivation {
name = if name != null then name else urlToName url revWithTag;
inherit name;

builder = ./builder.sh;
fetcher = ./nix-prefetch-git;
Expand Down
3 changes: 2 additions & 1 deletion pkgs/build-support/fetchgithub/default.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
lib,
repoRevToNameMaybe,
fetchgit,
fetchzip,
}:
Expand All @@ -10,7 +11,7 @@ lib.makeOverridable (
repo,
tag ? null,
rev ? null,
name ? "source",
name ? repoRevToNameMaybe repo (lib.revOrTag rev tag) "github",
fetchSubmodules ? false,
leaveDotGit ? null,
deepClone ? false,
Expand Down
8 changes: 6 additions & 2 deletions pkgs/build-support/fetchgitiles/default.nix
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
{ fetchzip, lib }:
{
fetchzip,
repoRevToNameMaybe,
lib,
}:

lib.makeOverridable (
{
url,
rev ? null,
tag ? null,
name ? "source",
name ? repoRevToNameMaybe url (lib.revOrTag rev tag) "gitiles",
...
}@args:

Expand Down
3 changes: 2 additions & 1 deletion pkgs/build-support/fetchgitlab/default.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
lib,
repoRevToNameMaybe,
fetchgit,
fetchzip,
}:
Expand All @@ -11,9 +12,9 @@ lib.makeOverridable (
repo,
rev ? null,
tag ? null,
name ? repoRevToNameMaybe repo (lib.revOrTag rev tag) "gitlab",
protocol ? "https",
domain ? "gitlab.com",
name ? "source",
group ? null,
fetchSubmodules ? false,
leaveDotGit ? false,
Expand Down
8 changes: 6 additions & 2 deletions pkgs/build-support/fetchrepoorcz/default.nix
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
{ fetchzip }:
{
lib,
repoRevToNameMaybe,
fetchzip,
}:

# gitweb example, snapshot support is optional in gitweb
{
repo,
rev,
name ? "source",
name ? repoRevToNameMaybe repo rev "repoorcz",
... # For hash agility
}@args:
fetchzip (
Expand Down
8 changes: 6 additions & 2 deletions pkgs/build-support/fetchsavannah/default.nix
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
{ fetchzip, lib }:
{
lib,
repoRevToNameMaybe,
fetchzip,
}:

lib.makeOverridable (
# cgit example, snapshot support is optional in cgit
{
repo,
rev,
name ? "source",
name ? repoRevToNameMaybe repo rev "savannah",
... # For hash agility
}@args:
fetchzip (
Expand Down
5 changes: 3 additions & 2 deletions pkgs/build-support/fetchsourcehut/default.nix
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
lib,
repoRevToNameMaybe,
fetchgit,
fetchhg,
fetchzip,
lib,
}:

let
Expand All @@ -18,9 +19,9 @@ makeOverridable (
owner,
repo,
rev,
name ? repoRevToNameMaybe repo rev "sourcehut",
domain ? "sr.ht",
vc ? "git",
name ? "source",
fetchSubmodules ? false,
... # For hash agility
}@args:
Expand Down
62 changes: 33 additions & 29 deletions pkgs/build-support/fetchsvn/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,55 @@
openssh ? null,
}:

let
repoToName =
url: rev:
let
inherit (lib)
removeSuffix
splitString
reverseList
head
last
elemAt
;
base = removeSuffix "/" (last (splitString ":" url));
path = reverseList (splitString "/" base);
repoName =
# ../repo/trunk -> repo
if head path == "trunk" then
elemAt path 1
# ../repo/branches/branch -> repo-branch
else if elemAt path 1 == "branches" then
"${elemAt path 2}-${head path}"
# ../repo/tags/tag -> repo-tag
else if elemAt path 1 == "tags" then
"${elemAt path 2}-${head path}"
# ../repo (no trunk) -> repo
else
head path;
in
"${repoName}-r${toString rev}";
in

{
url,
rev ? "HEAD",
name ? repoToName url rev,
sha256 ? "",
hash ? "",
ignoreExternals ? false,
ignoreKeywords ? false,
name ? null,
preferLocalBuild ? true,
}:

assert sshSupport -> openssh != null;

let
repoName =
let
fst = lib.head;
snd = l: lib.head (lib.tail l);
trd = l: lib.head (lib.tail (lib.tail l));
path_ =
(p: if lib.head p == "" then lib.tail p else p) # ~ drop final slash if any
(lib.reverseList (lib.splitString "/" url));
path = [ (lib.removeSuffix "/" (lib.head path_)) ] ++ (lib.tail path_);
in
# ../repo/trunk -> repo
if fst path == "trunk" then
snd path
# ../repo/branches/branch -> repo-branch
else if snd path == "branches" then
"${trd path}-${fst path}"
# ../repo/tags/tag -> repo-tag
else if snd path == "tags" then
"${trd path}-${fst path}"
# ../repo (no trunk) -> repo
else
fst path;

name_ = if name == null then "${repoName}-r${toString rev}" else name;
in

if hash != "" && sha256 != "" then
throw "Only one of sha256 or hash can be set"
else
stdenvNoCC.mkDerivation {
name = name_;
inherit name;
builder = ./builder.sh;
nativeBuildInputs = [
cacert
Expand Down
Loading