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

POC of overlays approach to customization #137

Closed
wants to merge 2 commits into from
Closed
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
10 changes: 9 additions & 1 deletion default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
{ sources ? import ./nix/sources.nix
let
overlay = self: super: {
nixpkgs = super.nixpkgs-renamed;
__fetchers = super.__fetchers // {
fetchgit = {url, sha256}@attrs: (import self.nixpkgs {}).fetchgit attrs;
};
};
in
{ sources ? import ./nix/sources.nix { overlays = [overlay]; }
, pkgs ? import ./nix { inherit sources; }
}:

Expand Down
11 changes: 3 additions & 8 deletions nix/default.nix
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
{ sources ? import ./sources.nix }:
{ sources ? import ./sources.nix {} }:
import sources.nixpkgs
{ overlays =
[ (_: pkgs:
{ inherit sources; }
)
(_: pkgs:
{ termtosvg = pkgs.callPackage ./termtosvg.nix {}; }
)

[ (_: _: { inherit sources; })
(_: super: { termtosvg = super.callPackage ./termtosvg.nix {}; })
];
config = {};
}
9 changes: 4 additions & 5 deletions nix/sources.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"nixpkgs": {
"nixpkgs-renamed": {
"branch": "nixos-19.03",
"description": "Nixpkgs/NixOS branches that track the Nixpkgs/NixOS channels",
"homepage": "https://github.com/NixOS/nixpkgs",
"owner": "NixOS",
"repo": "nixpkgs-channels",
"rev": "5b0b58685d68b9c9c12132f7b392c9dcdecc5931",
"sha256": "1fy1ghl79p3w1vg0hnakvrb1lqic4mjrwmylcgqa5bd4ilfrirzf",
"type": "tarball",
"type": "builtin-tarball",
"url": "https://github.com/NixOS/nixpkgs-channels/archive/5b0b58685d68b9c9c12132f7b392c9dcdecc5931.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
Expand All @@ -19,8 +19,7 @@
"repo": "termtosvg",
"rev": "b97cc0132073111cec37f64c95539e869202ff99",
"sha256": "1c86622sda98zm3q3dzwmm37i09yca661kn860svl191nqbh2l68",
"type": "tarball",
"url": "https://github.com/nbedos/termtosvg/archive/b97cc0132073111cec37f64c95539e869202ff99.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
"type": "fetchgit",
"url": "https://github.com/nbedos/termtosvg.git"
}
}
146 changes: 64 additions & 82 deletions nix/sources.nix
Original file line number Diff line number Diff line change
@@ -1,93 +1,75 @@
# 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.
'';

# fetchTarball version that is compatible between all the versions of Nix
builtins_fetchTarball =
{ url, sha256 }@attrs:
let
inherit (builtins) lessThan nixVersion fetchTarball;
in
if lessThan nixVersion "1.12" then
fetchTarball { inherit url; }
else
fetchTarball attrs;

# fetchurl version that is compatible between all the versions of Nix
builtins_fetchurl =
{ url, sha256 }@attrs:
let
inherit (builtins) lessThan nixVersion fetchurl;
in
if lessThan nixVersion "1.12" then
fetchurl { inherit url; }
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;

# A wrapper around pkgs.fetchurl that has inspectable arguments,
# annoyingly this means we have to specify them
fetchurl = { url, sha256 }@attrs: pkgs.fetchurl attrs;
{ overlays ? [] }:
let
# borrowed from nixpkgs
extends = f: rattrs: self: let super = rattrs self; in super // f self super;
fix = f: let x = f x; in x;
functionArgs = f: f.__functionArgs or (builtins.functionArgs f);
flip = f: a: b: f b a;
callFunctionWith = autoArgs: f: args:
let auto = builtins.intersectAttrs (functionArgs f) autoArgs;
in f (auto // args);
mapAttrs = builtins.mapAttrs or
(f: set: with builtins;
listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set)));

hasNixpkgsPath = (builtins.tryEval <nixpkgs>).success;
hasThisAsNixpkgsPath =
(builtins.tryEval <nixpkgs>).success && <nixpkgs> == ./.;

sources = builtins.fromJSON (builtins.readFile ./sources.json);
niv-overlay = self: super: {
nixpkgs =
if hasNixpkgsPath && !hasThisAsNixpkgsPath
then <nixpkgs>
else super.nixpkgs;

mapAttrs = builtins.mapAttrs or
(f: set: with builtins;
listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set)));
__fetchers = {
# A wrapper around pkgs.fetchzip that has inspectable arguments,
# annoyingly this means we have to specify them
tarball = { url, sha256 }@attrs: (import self.nixpkgs {}).fetchzip attrs;
# fetchTarball version that is compatible between all the versions of Nix
builtin-tarball = { url, sha256 }@attrs:
let inherit (builtins) lessThan nixVersion fetchTarball;
in
if lessThan nixVersion "1.12"
then fetchTarball { inherit url; }
else fetchTarball attrs;
# A wrapper around pkgs.fetchurl that has inspectable arguments,
# annoyingly this means we have to specify them
file = { url, sha256 }@attrs: (import self.nixpkgs {}).fetchurl attrs;
# fetchurl version that is compatible between all the versions of Nix
builtin-url = { url, sha256 }@attrs:
let inherit (builtins) lessThan nixVersion fetchurl;
in
if lessThan nixVersion "1.12"
then fetchurl { inherit url; }
else fetchurl attrs;
};
};

# 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);
sources = builtins.fromJSON (builtins.readFile ./sources.json);

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
sources-overlay = self: super:
let getFetcher = spec:
let fetcherName =
if builtins.hasAttr "type" spec
then builtins.getAttr "type" spec
else "builtin-tarball";
in builtins.getAttr fetcherName (self.__fetchers);
in mapAttrs (_: spec:
# NOTE: spec must _not_ have an "outPath" attribute
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;
toFix = builtins.foldl' (flip extends) (self: {}) ([ niv-overlay sources-overlay ] ++ overlays);
in
fix toFix
1 change: 0 additions & 1 deletion script/test
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env nix-shell
#!nix-shell -i bash
#!nix-shell -I nixpkgs=./nix
#!nix-shell -p nix
#!nix-shell --pure
#!nix-shell --keep SSL_CERT_FILE
Expand Down