From 24255eab8d1fefd970299147ffc2da2b7dbce03a Mon Sep 17 00:00:00 2001 From: Tyson Whitehead Date: Thu, 18 Jan 2018 00:11:48 -0500 Subject: [PATCH] Overlays based framework for extending nixpkgs --- default.nix | 92 +++++++++++++++++++++++++++++++++++++++++++ pkgs/all-packages.nix | 3 ++ 2 files changed, 95 insertions(+) create mode 100644 default.nix create mode 100644 pkgs/all-packages.nix diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..a5948bc --- /dev/null +++ b/default.nix @@ -0,0 +1,92 @@ +with builtins; + +# This duplicates the overlay code in nixpkgs/pkgs/top-level/impure.nix (may require periodic resyncing). +# +# It has to be done this way because passing overlays to nixpkgs (required to insert our packages) disables all the +# default overlay mechanisms (required to pickup any user or system overlays). + +let + + # overlaysDefault - List of overlay functions nixpkgs would normally use if not provided. + # + # This is the order overlays are searched for in accordance to what is done in nixpkgs. If an overlay is found + # at a given level (the leading number) the search is stopped and they are used. If multiple overlays are found + # at a given level then an error is generated. + # + # 1: (i.e., nixpkgs-overlays.nix or nixpkgs-overlays/default.nix in NIX_PATH) + # 2: ~/.config/nixpkgs/overlays.nix + # 2: ~/.config/nixpkgs/overlays + # + # This code is lifted directly from nixpkgs/pkgs/top-level/impure.nix to ensure consistency. + # + overlaysDefault = + let + + # isDir path - Is path a directory (requires access). + isDir = path: pathExists (path + "/."); + + # try expression default - Replace expression with default on exception. + try = x: def: let res = tryEval x; in if res.success then res.value else def; + + # overlaysRetrieve path - Retrieve a list of the overlay functions from path. + # path is file - import the file itself (should give a list of overlay functions) + # path is directory - list of imports of all the *.nix files in the directory (each should give an overlay function) + # + overlaysRetrieve = path: + if isDir path then + let content = readDir path; in + map (n: import (path + ("/" + n))) + (builtins.filter (n: builtins.match ".*\\.nix" n != null || pathExists (path + ("/" + n + "/default.nix"))) + (attrNames content)) + else + import path; + + # pathOverlays - NIX_PATH nixpkgs-overlays file or "" if not found + pathOverlays = try ""; + + # homeDir - ~/ + # homeOverlaysFile - ~/.config/nixpkgs/overlays.nix + # homeOverlaysDir - ~/.config/nixpkgs/overlays + # + homeDir = builtins.getEnv "HOME"; + homeOverlaysFile = homeDir + "/.config/nixpkgs/overlays.nix"; + homeOverlaysDir = homeDir + "/.config/nixpkgs/overlays"; + + in + if pathOverlays != "" && pathExists pathOverlays then overlaysRetrieve pathOverlays + else if pathExists homeOverlaysFile && pathExists homeOverlaysDir then + throw '' + Nixpkgs overlays can be specified with ${homeOverlaysFile} or ${homeOverlaysDir}, but not both. + Please remove one of them and try again. + '' + else if pathExists homeOverlaysFile then + if isDir homeOverlaysFile then + throw (homeOverlaysFile + " should be a file") + else overlaysRetrieve homeOverlaysFile + else if pathExists homeOverlaysDir then + if !(isDir homeOverlaysDir) then + throw (homeOverlaysDir + " should be a directory") + else overlaysRetrieve homeOverlaysDir + else []; + + + # overlaysAlways - List of overlay functions provide by this package. + # + # The naming in this function is misleading but consistent with nixpkgs. + # + # self - final package set (stack fully closed) (called pkgs in all-packages.nix) + # super - prior package set (stack closed up to us) + # res - next package set (stack closed up to and over us) (called self in all-packages.nix_ + # + overlaysAlways = [(self: super: + let + res = import ./pkgs/all-packages.nix res self; + in + res + )]; + +in + + { overlays ? overlaysDefault + , ... } @ args: + import ./nixpkgs (args // { overlays = overlaysAlways ++ overlays; }) diff --git a/pkgs/all-packages.nix b/pkgs/all-packages.nix new file mode 100644 index 0000000..21840a2 --- /dev/null +++ b/pkgs/all-packages.nix @@ -0,0 +1,3 @@ +self: pkgs: with pkgs; { + +}