npins/sources.nix: format

This commit is contained in:
alfarel 2026-01-15 00:04:46 +00:00
commit c7dddbb387
No known key found for this signature in database

View file

@ -1,27 +1,32 @@
/* /*
This file is provided under the MIT licence: This file is provided under the MIT licence:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
# Generated by npins. Do not modify; will be overwritten regularly # Generated by npins. Do not modify; will be overwritten regularly
let let
# Backwards-compatibly make something that previously didn't take any arguments take some # Backwards-compatibly make something that previously didn't take any arguments take some
# The function must return an attrset, and will unfortunately be eagerly evaluated # The function must return an attrset, and will unfortunately be eagerly evaluated
# Same thing, but it catches eval errors on the default argument so that one may still call it with other arguments # Same thing, but it catches eval errors on the default argument so that one may still call it with other arguments
mkFunctor = mkFunctor = fn: let
fn: e = builtins.tryEval (fn {});
let in
e = builtins.tryEval (fn { }); (
in if e.success
(if e.success then e.value else { error = fn { }; }) // { __functor = _self: fn; }; then e.value
else {error = fn {};}
)
// {__functor = _self: fn;};
# https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295 # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295
range = range = first: last:
first: last: if first > last then [ ] else builtins.genList (n: first + n) (last - first + 1); if first > last
then []
else builtins.genList (n: first + n) (last - first + 1);
# https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257 # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257
stringToCharacters = s: map (p: builtins.substring p 1 s) (range 0 (builtins.stringLength s - 1)); stringToCharacters = s: map (p: builtins.substring p 1 s) (range 0 (builtins.stringLength s - 1));
@ -33,188 +38,173 @@ let
# If the environment variable NPINS_OVERRIDE_${name} is set, then use # If the environment variable NPINS_OVERRIDE_${name} is set, then use
# the path directly as opposed to the fetched source. # the path directly as opposed to the fetched source.
# (Taken from Niv for compatibility) # (Taken from Niv for compatibility)
mayOverride = mayOverride = name: path: let
name: path: envVarName = "NPINS_OVERRIDE_${saneName}";
let saneName = stringAsChars (c:
envVarName = "NPINS_OVERRIDE_${saneName}"; if (builtins.match "[a-zA-Z0-9]" c) == null
saneName = stringAsChars (c: if (builtins.match "[a-zA-Z0-9]" c) == null then "_" else c) name; then "_"
ersatz = builtins.getEnv envVarName; else c)
in name;
if ersatz == "" then ersatz = builtins.getEnv envVarName;
path in
if ersatz == ""
then path
else else
# this turns the string into an actual Nix path (for both absolute and # this turns the string into an actual Nix path (for both absolute and
# relative paths) # relative paths)
builtins.trace "Overriding path of \"${name}\" with \"${ersatz}\" due to set \"${envVarName}\"" ( builtins.trace "Overriding path of \"${name}\" with \"${ersatz}\" due to set \"${envVarName}\"" (
if builtins.substring 0 1 ersatz == "/" then if builtins.substring 0 1 ersatz == "/"
/. + ersatz then /. + ersatz
else else /. + builtins.getEnv "PWD" + "/${ersatz}"
/. + builtins.getEnv "PWD" + "/${ersatz}"
); );
mkSource = mkSource = name: spec: {pkgs ? null}:
name: spec: assert spec ? type; let
{
pkgs ? null,
}:
assert spec ? type;
let
# Unify across builtin and pkgs fetchers. # Unify across builtin and pkgs fetchers.
# `fetchGit` requires a wrapper because of slight API differences. # `fetchGit` requires a wrapper because of slight API differences.
fetchers = fetchers =
if pkgs == null then if pkgs == null
{ then {
inherit (builtins) fetchTarball fetchurl; inherit (builtins) fetchTarball fetchurl;
# For some fucking reason, fetchGit has a different signature than the other builtin fetchers … # For some fucking reason, fetchGit has a different signature than the other builtin fetchers …
fetchGit = args: (builtins.fetchGit args).outPath; fetchGit = args: (builtins.fetchGit args).outPath;
} }
else else {
{ fetchTarball = {
fetchTarball = url,
{ sha256,
url, }:
sha256, pkgs.fetchzip {
}: inherit url sha256;
pkgs.fetchzip { extension = "tar";
inherit url sha256; };
extension = "tar"; inherit (pkgs) fetchurl;
}; fetchGit = {
inherit (pkgs) fetchurl; url,
fetchGit = submodules,
{ rev,
url, name,
submodules, narHash,
rev, }:
name, pkgs.fetchgit {
narHash, inherit url rev name;
}: fetchSubmodules = submodules;
pkgs.fetchgit { hash = narHash;
inherit url rev name; };
fetchSubmodules = submodules; };
hash = narHash;
};
};
# Dispatch to the correct code path based on the type # Dispatch to the correct code path based on the type
path = path =
if spec.type == "Git" then if spec.type == "Git"
mkGitSource fetchers spec then mkGitSource fetchers spec
else if spec.type == "GitRelease" then else if spec.type == "GitRelease"
mkGitSource fetchers spec then mkGitSource fetchers spec
else if spec.type == "PyPi" then else if spec.type == "PyPi"
mkPyPiSource fetchers spec then mkPyPiSource fetchers spec
else if spec.type == "Channel" then else if spec.type == "Channel"
mkChannelSource fetchers spec then mkChannelSource fetchers spec
else if spec.type == "Tarball" then else if spec.type == "Tarball"
mkTarballSource fetchers spec then mkTarballSource fetchers spec
else if spec.type == "Container" then else if spec.type == "Container"
mkContainerSource pkgs spec then mkContainerSource pkgs spec
else else builtins.throw "Unknown source type ${spec.type}";
builtins.throw "Unknown source type ${spec.type}";
in in
spec // { outPath = mayOverride name path; }; spec // {outPath = mayOverride name path;};
mkGitSource = mkGitSource = {
{ fetchTarball,
fetchTarball, fetchGit,
fetchGit, ...
... }: {
}: repository,
{ revision,
repository, url ? null,
revision, submodules,
url ? null, hash,
submodules, ...
hash, }:
...
}:
assert repository ? type; assert repository ? type;
# At the moment, either it is a plain git repository (which has an url), or it is a GitHub/GitLab repository # At the moment, either it is a plain git repository (which has an url), or it is a GitHub/GitLab repository
# In the latter case, there we will always be an url to the tarball # In the latter case, there we will always be an url to the tarball
if url != null && !submodules then if url != null && !submodules
fetchTarball { then
inherit url; fetchTarball {
sha256 = hash; inherit url;
} sha256 = hash;
else }
let else let
url = url =
if repository.type == "Git" then if repository.type == "Git"
repository.url then repository.url
else if repository.type == "GitHub" then else if repository.type == "GitHub"
"https://github.com/${repository.owner}/${repository.repo}.git" then "https://github.com/${repository.owner}/${repository.repo}.git"
else if repository.type == "GitLab" then else if repository.type == "GitLab"
"${repository.server}/${repository.repo_path}.git" then "${repository.server}/${repository.repo_path}.git"
else if repository.type == "Forgejo" then else if repository.type == "Forgejo"
"${repository.server}/${repository.owner}/${repository.repo}.git" then "${repository.server}/${repository.owner}/${repository.repo}.git"
else else throw "Unrecognized repository type ${repository.type}";
throw "Unrecognized repository type ${repository.type}"; urlToName = url: rev: let
urlToName = matched = builtins.match "^.*/([^/]*)(\\.git)?$" url;
url: rev:
let
matched = builtins.match "^.*/([^/]*)(\\.git)?$" url;
short = builtins.substring 0 7 rev; short = builtins.substring 0 7 rev;
appendShort = if (builtins.match "[a-f0-9]*" rev) != null then "-${short}" else ""; appendShort =
in if (builtins.match "[a-f0-9]*" rev) != null
"${if matched == null then "source" else builtins.head matched}${appendShort}"; then "-${short}"
else "";
in "${
if matched == null
then "source"
else builtins.head matched
}${appendShort}";
name = urlToName url revision; name = urlToName url revision;
in in
fetchGit { fetchGit {
rev = revision; rev = revision;
narHash = hash; narHash = hash;
inherit name submodules url; inherit name submodules url;
}; };
mkPyPiSource = mkPyPiSource = {fetchurl, ...}: {
{ fetchurl, ... }: url,
{ hash,
url, ...
hash, }:
...
}:
fetchurl { fetchurl {
inherit url; inherit url;
sha256 = hash; sha256 = hash;
}; };
mkChannelSource = mkChannelSource = {fetchTarball, ...}: {
{ fetchTarball, ... }: url,
{ hash,
url, ...
hash, }:
...
}:
fetchTarball { fetchTarball {
inherit url; inherit url;
sha256 = hash; sha256 = hash;
}; };
mkTarballSource = mkTarballSource = {fetchTarball, ...}: {
{ fetchTarball, ... }: url,
{ locked_url ? url,
url, hash,
locked_url ? url, ...
hash, }:
...
}:
fetchTarball { fetchTarball {
url = locked_url; url = locked_url;
sha256 = hash; sha256 = hash;
}; };
mkContainerSource = mkContainerSource = pkgs: {
pkgs: image_name,
{ image_tag,
image_name, image_digest,
image_tag, ...
image_digest, }:
... if pkgs == null
}: then builtins.throw "container sources require passing in a Nixpkgs value: https://github.com/andir/npins/blob/master/README.md#using-the-nixpkgs-fetchers"
if pkgs == null then
builtins.throw "container sources require passing in a Nixpkgs value: https://github.com/andir/npins/blob/master/README.md#using-the-nixpkgs-fetchers"
else else
pkgs.dockerTools.pullImage { pkgs.dockerTools.pullImage {
imageName = image_name; imageName = image_name;
@ -222,28 +212,23 @@ let
finalImageTag = image_tag; finalImageTag = image_tag;
}; };
in in
mkFunctor ( mkFunctor (
{ {input ? ./sources.json}: let
input ? ./sources.json, data =
}: if builtins.isPath input
let then
data = # while `readFile` will throw an error anyways if the path doesn't exist,
if builtins.isPath input then # we still need to check beforehand because *our* error can be caught but not the one from the builtin
# while `readFile` will throw an error anyways if the path doesn't exist, # *piegames sighs*
# we still need to check beforehand because *our* error can be caught but not the one from the builtin if builtins.pathExists input
# *piegames sighs* then builtins.fromJSON (builtins.readFile input)
if builtins.pathExists input then else throw "Input path ${toString input} does not exist"
builtins.fromJSON (builtins.readFile input) else if builtins.isAttrs input
else then input
throw "Input path ${toString input} does not exist" else throw "Unsupported input type ${builtins.typeOf input}, must be a path or an attrset";
else if builtins.isAttrs input then version = data.version;
input in
else if version == 7
throw "Unsupported input type ${builtins.typeOf input}, must be a path or an attrset"; then builtins.mapAttrs (name: spec: mkFunctor (mkSource name spec)) data.pins
version = data.version; else throw "Unsupported format version ${toString version} in sources.json. Try running `npins upgrade`"
in )
if version == 7 then
builtins.mapAttrs (name: spec: mkFunctor (mkSource name spec)) data.pins
else
throw "Unsupported format version ${toString version} in sources.json. Try running `npins upgrade`"
)