Skip to content

Commit

Permalink
Move niv stuff out of nix/*
Browse files Browse the repository at this point in the history
  • Loading branch information
cprussin committed Dec 8, 2019
1 parent 74a6016 commit 4fbdc79
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 32 deletions.
2 changes: 1 addition & 1 deletion default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ sources ? import ./nix/sources.nix }:
{ sources ? import ./niv-sources.nix }:

let
pkgs = import sources.nixpkgs {};
Expand Down
3 changes: 3 additions & 0 deletions niv-sources.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import ./sources.nix {
sourcesFile = ./sources.json;
}
2 changes: 1 addition & 1 deletion pkgs/dircolors-solarized/default.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{ stdenv }:

let
sources = import ../../nix/sources.nix;
sources = import ../../niv-sources.nix;
in

stdenv.mkDerivation {
Expand Down
2 changes: 1 addition & 1 deletion pkgs/notify-send/default.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{ stdenv, lib, makeWrapper, glib }:

let
sources = import ../../nix/sources.nix;
sources = import ../../niv-sources.nix;
in

stdenv.mkDerivation {
Expand Down
2 changes: 1 addition & 1 deletion pkgs/zoom-frm/default.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{ epkgs }:

let
sources = import ../../nix/sources.nix;
sources = import ../../niv-sources.nix;
in

epkgs.trivialBuild {
Expand Down
2 changes: 1 addition & 1 deletion pkgs/zsh-git-prompt/default.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{ stdenv, makeWrapper, python, git }:

let
sources = import ../../nix/sources.nix;
sources = import ../../niv-sources.nix;
in

stdenv.mkDerivation rec {
Expand Down
18 changes: 14 additions & 4 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
{ sources ? import ./nix/sources.nix }:
{ sources ? import ./niv-sources.nix }:

let
pkgs = import sources.nixpkgs {};

niv = import sources.niv {};
nivPkgs = import sources.niv {};

niv = pkgs.symlinkJoin {
name = "niv";
paths = [ nivPkgs.niv ];
buildInputs = [ pkgs.makeWrapper ];
postBuild = ''
wrapProgram $out/bin/niv \
--add-flags "--sources-json ${toString ./sources.json}"
'';
};

nix-linter = pkgs.callPackage sources.nix-linter {};

Expand All @@ -19,7 +29,7 @@ let
home-manager = sources.home-manager;
};

files = "$(find . -name '*.nix' -not -path './nix/*')";
files = "$(find . -name '*.nix' -not -wholename './sources.nix')";

lint = pkgs.writeShellScriptBin "lint" "nix-linter ${files}";

Expand Down Expand Up @@ -66,7 +76,7 @@ pkgs.mkShell {
buildInputs = [
pkgs.git
pkgs.nixpkgs-fmt
niv.niv
niv
nix-linter.nix-linter
lint
format
Expand Down
File renamed without changes.
54 changes: 31 additions & 23 deletions nix/sources.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); }

0 comments on commit 4fbdc79

Please sign in to comment.