Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom location for nix/sources.json #159

Merged
merged 6 commits into from
Dec 8, 2019
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [0.2.7] 2019-12-08
## Added
* Support for custom path `sources.json` with `--sources-json`

## [0.2.6] 2019-12-05
## Changed
* Fix `niv update` with `git` specs
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,12 @@ $ niv update ghc -v 8.6.2
```
niv - dependency manager for Nix projects

version: 0.2.6
version: 0.2.7

Usage: niv COMMAND
Usage: niv [-s|--sources-json FILE] COMMAND

Available options:
-s,--sources-json FILE Use FILE instead of nix/sources.json
-h,--help Show this help text

Available commands:
Expand Down
54 changes: 31 additions & 23 deletions nix/sources.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ let
# The fetchers. fetch_<type> fetches specs of type <type>.
#

fetch_file = spec:
fetch_file = pkgs: spec:
if spec.builtin or true then
builtins_fetchurl { inherit (spec) url sha256; }
else
pkgs.fetchurl { inherit (spec) url sha256; };

fetch_tarball = spec:
fetch_tarball = pkgs: spec:
if spec.builtin or true then
builtins_fetchTarball { inherit (spec) url sha256; }
else
Expand Down Expand Up @@ -43,27 +43,21 @@ let
''
(builtins_fetchurl { inherit (spec) url sha256; });

#
# The sources to fetch.
#

sources = builtins.fromJSON (builtins.readFile ./sources.json);

#
# Various helpers
#

# The set of packages used when specs are fetched using non-builtins.
pkgs =
mkPkgs = sources:
if hasNixpkgsPath
then
if hasThisAsNixpkgsPath
then import (builtins_fetchTarball { inherit (sources_nixpkgs) url sha256; }) {}
then import (builtins_fetchTarball { inherit (mkNixpkgs sources) url sha256; }) {}
else import <nixpkgs> {}
else
import (builtins_fetchTarball { inherit (sources_nixpkgs) url sha256; }) {};
import (builtins_fetchTarball { inherit (mkNixpkgs sources) url sha256; }) {};

sources_nixpkgs =
mkNixpkgs = sources:
if builtins.hasAttr "nixpkgs" sources
then sources.nixpkgs
else abort
Expand All @@ -77,12 +71,12 @@ let
(builtins.tryEval <nixpkgs>).success && <nixpkgs> == ./.;

# The actual fetching function.
fetch = name: spec:
fetch = pkgs: name: spec:

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 spec
else if spec.type == "tarball" then fetch_tarball spec
else if spec.type == "file" then fetch_file pkgs spec
else if spec.type == "tarball" then fetch_tarball pkgs spec
else if spec.type == "git" then fetch_git spec
else if spec.type == "builtin-tarball" then fetch_builtin-tarball spec
else if spec.type == "builtin-url" then fetch_builtin-url spec
Expand Down Expand Up @@ -117,12 +111,26 @@ let
else
fetchurl attrs;

# Create the final "sources" from the config
mkSources = config:
mapAttrs (
name: spec:
if builtins.hasAttr "outPath" spec
then abort
"The values in sources.json should not have an 'outPath' attribute"
else
spec // { outPath = fetch config.pkgs name spec; }
) config.sources;

# The "config" used by the fetchers
mkConfig =
{ sourcesFile ? ./sources.json
}: rec {
# The sources, i.e. the attribute set of spec name to spec
sources = builtins.fromJSON (builtins.readFile sourcesFile);
# The "pkgs" (evaluated nixpkgs) to use for e.g. non-builtin fetchers
pkgs = mkPkgs sources;
};
in
mapAttrs (
name: spec:
if builtins.hasAttr "outPath" spec
then abort
"The values in sources.json should not have an 'outPath' attribute"
else
spec // { outPath = fetch name spec; }
) sources
mkSources (mkConfig {}) //
{ __functor = _: settings: mkSources (mkConfig settings); }
2 changes: 1 addition & 1 deletion package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: niv
version: 0.2.6
version: 0.2.7
license: MIT
author: Nicolas Mattia <[email protected]>
maintainer: Nicolas Mattia <[email protected]>
Expand Down
5 changes: 3 additions & 2 deletions src/Data/Text/Extended.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
module Data.Text.Extended where

import Niv.Logger
import UnliftIO
import System.Exit (exitFailure)
import qualified Data.Text as T

tshow :: Show a => a -> T.Text
tshow = T.pack . show

-- not quite the perfect place for this
abort :: T.Text -> IO a
abort :: MonadIO io => T.Text -> io a
abort msg = do
tsay $ T.unwords [ tbold $ tred "FATAL:", msg ]
exitFailure
liftIO exitFailure
Loading