1
1
mirror of https://github.com/nmattia/niv.git synced 2024-10-06 12:27:35 +03:00
This commit is contained in:
Nicolas Mattia 2019-10-29 09:42:34 +01:00
parent 1dd094156b
commit b69df92ce1
2 changed files with 83 additions and 62 deletions

View File

@ -1,25 +1,11 @@
# This file has been generated by Niv.
# A record, from name to path, of the third-party packages
with rec
{
pkgs =
if hasNixpkgsPath
then
if hasThisAsNixpkgsPath
then import (builtins_fetchTarball { inherit (sources_nixpkgs) url sha256; }) {}
else import <nixpkgs> {}
else
import (builtins_fetchTarball { inherit (sources_nixpkgs) url sha256; }) {};
sources_nixpkgs =
if builtins.hasAttr "nixpkgs" sources
then sources.nixpkgs
else abort
''
Please specify either <nixpkgs> (through -I or NIX_PATH=nixpkgs=...) or
add a package called "nixpkgs" to your sources.json.
'';
let
readJSON = fp: builtins.fromJSON (builtins.readFile fp);
hasNixpkgsPath = (builtins.tryEval <nixpkgs>).success;
hasThisAsNixpkgsPath =
(builtins.tryEval <nixpkgs>).success && <nixpkgs> == ./.;
# fetchTarball version that is compatible between all the versions of Nix
builtins_fetchTarball =
@ -43,51 +29,84 @@ with rec
else
fetchurl attrs;
# A wrapper around pkgs.fetchzip that has inspectable arguments,
# annoyingly this means we have to specify them
fetchzip = { url, sha256 }@attrs: pkgs.fetchzip attrs;
mk =
{ sourcesJson ? ./sources.json,
useBuiltinFetchers ? true,
pkgs ?
let
sources = readJSON sourcesJson;
sources_nixpkgs =
if builtins.hasAttr "nixpkgs" sources
then sources.nixpkgs
else abort
''
nixpkgs is needed, but I cannot find one to use.
Please either
* specify <nixpkgs> (through -I or NIX_PATH=nixpkgs=...) or
* add a package called "nixpkgs" to your sources.json or
* use `sources.__configure { pkgs = ... }` or
* use `sources.__configure { useBuiltinFetchers = true; }`
'';
in
if hasNixpkgsPath
then
if hasThisAsNixpkgsPath
then import (builtins_fetchTarball { inherit (sources_nixpkgs) url sha256; }) {}
else import <nixpkgs> {}
else
import (builtins_fetchTarball { inherit (sources_nixpkgs) url sha256; }) {}
}:
# A wrapper around pkgs.fetchurl that has inspectable arguments,
# annoyingly this means we have to specify them
fetchurl = { url, sha256 }@attrs: pkgs.fetchurl attrs;
let
sources = readJSON sourcesJson;
hasNixpkgsPath = (builtins.tryEval <nixpkgs>).success;
hasThisAsNixpkgsPath =
(builtins.tryEval <nixpkgs>).success && <nixpkgs> == ./.;
# A wrapper around pkgs.fetchzip that has inspectable arguments,
# annoyingly this means we have to specify them
fetchzip = { url, sha256 }@attrs: pkgs.fetchzip attrs;
sources = builtins.fromJSON (builtins.readFile ./sources.json);
# A wrapper around pkgs.fetchurl that has inspectable arguments,
# annoyingly this means we have to specify them
fetchurl = { url, sha256 }@attrs: pkgs.fetchurl attrs;
mapAttrs = builtins.mapAttrs or
(f: set: with builtins;
listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set)));
mapAttrs = builtins.mapAttrs or
(f: set: with builtins;
listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set)));
# borrowed from nixpkgs
functionArgs = f: f.__functionArgs or (builtins.functionArgs f);
callFunctionWith = autoArgs: f: args:
let auto = builtins.intersectAttrs (functionArgs f) autoArgs;
in f (auto // args);
# borrowed from nixpkgs
# TODO: get rid of this
functionArgs = f: f.__functionArgs or (builtins.functionArgs f);
callFunctionWith = autoArgs: f: args:
let auto = builtins.intersectAttrs (functionArgs f) autoArgs;
in f (auto // args);
getFetcher = spec:
let fetcherName =
if builtins.hasAttr "type" spec
then builtins.getAttr "type" spec
else "builtin-tarball";
in builtins.getAttr fetcherName {
"tarball" = fetchzip;
"builtin-tarball" = builtins_fetchTarball;
"file" = fetchurl;
"builtin-url" = builtins_fetchurl;
};
};
# NOTE: spec must _not_ have an "outPath" attribute
mapAttrs (_: spec:
if builtins.hasAttr "outPath" spec
then abort
"The values in sources.json should not have an 'outPath' attribute"
else
if builtins.hasAttr "url" spec && builtins.hasAttr "sha256" spec
then
spec //
{ outPath = callFunctionWith spec (getFetcher spec) { }; }
else spec
) sources
getFetcher = spec:
# TODO:
# here use
# if spec.type == ... then ... else if spec.type == ... etc and
# finally abort with error saying "please use __configure { fetcher =
# ... }". If `! spec ? type` then abort.
let fetcherName = spec.type or "builtin-tarball";
in builtins.getAttr fetcherName {
"tarball" = if useBuiltinFetchers then builtins_fetchTarball else fetchzip;
# TODO: message about deprecation
"builtin-tarball" = builtins_fetchTarball;
"file" = if useBuiltinFetchers then builtins_fetchurl else fetchurl;
"builtin-url" = builtins_fetchurl;
};
in
# NOTE: spec must _not_ have an "outPath" attribute
(mapAttrs (_: spec:
if builtins.hasAttr "outPath" spec
then abort
"The values in sources.json should not have an 'outPath' attribute"
else
if builtins.hasAttr "url" spec && builtins.hasAttr "sha256" spec
then
spec //
{ outPath = callFunctionWith spec (getFetcher spec) { }; }
else spec
) sources )//
{ __configure = mk ; };
in
mk {}

View File

@ -46,6 +46,7 @@ cli = join $ Opts.execParser opts
opts = Opts.info (parseCommand <**> Opts.helper) $ mconcat desc
desc =
[ Opts.fullDesc
-- , Opts.progDesc "FOOO"
, Opts.headerDoc $ Just $
"niv - dependency manager for Nix projects" Opts.<$$>
"" Opts.<$$>
@ -59,7 +60,8 @@ parseCommand = Opts.subparser (
Opts.command "show" parseCmdShow <>
Opts.command "update" parseCmdUpdate <>
Opts.command "modify" parseCmdModify <>
Opts.command "drop" parseCmdDrop )
Opts.command "drop" parseCmdDrop ) <|>
(pure () <$ Opts.abortOption Opts.ShowHelpText (Opts.helpDoc (Just "foo")))
newtype Sources = Sources
{ unSources :: HMS.HashMap PackageName PackageSpec }