~aasg/nixexprs

9749780caf8852c8fb2ca4d6a3ad816f6f1be4d7 — Aluísio Augusto Silva Gonçalves 27 days ago b3a95d9
Redefine package set on top of overlays

I wanted to reduce duplication, but ended up somewhat in the same place
(with manifest.nix).  Yet I find it much more elegant.
M ci.nix => ci.nix +5 -41
@@ 1,19 1,7 @@
{ pkgs ? import <nixpkgs> { }
, flake-utils-lib ? import (fetchTarball {
    url = "https://github.com/numtide/flake-utils/archive/588973065fce51f4763287f0fda87a174d78bf48.tar.gz";
    sha256 = "17h8rcp246y3444l9fp92jz1h5gp4gvgpnnd8rmhq686mdbha02r";
  })
}:
{ pkgs ? import <nixpkgs> { } }:
let
  inherit (builtins) deepSeq concatStringsSep listToAttrs mapAttrs;
  inherit (pkgs) recurseIntoAttrs;
  inherit (pkgs.lib.attrsets) filterAttrs getAttrFromPath isDerivation mapAttrsToList nameValuePair;
  inherit (pkgs.lib.strings) splitString;
  inherit (pkgs.lib.trivial) flip pipe;

  newPackages = selectDerivations (import ./pkgs { inherit pkgs; });

  patchedPackages = selectDerivations (import ./patches { pkgs = pkgs.extend (import ./pkgs/overlay.nix); });
  inherit (pkgs.lib) deepSeq filterAttrs isDerivation mapAttrs pipe;

  selectDerivations = set:
    let


@@ 31,36 19,12 @@ let
    in
    derivationTree (recurseIntoAttrs set);

  flattenAttrsFromPaths = paths: set:
    listToAttrs
      (map
        (path: nameValuePair (concatStringsSep "__" path) (getAttrFromPath path set))
        paths);

  packagePaths = (flip pipe) [
    flake-utils-lib.flattenTree
    (mapAttrsToList (name: _: splitString "/" name))
  ];

  self = import ./. { inherit pkgs; };
in
{
  lib = deepSeq (import ./lib/tests.nix { lib = pkgs.lib; }) { };

  newPackagesDirect = newPackages;

  newPackagesOverlay = pipe [ ./pkgs/overlay.nix ] [
    (map import)
    pkgs.appendOverlays
    (flattenAttrsFromPaths (packagePaths newPackages))
    recurseIntoAttrs
  ];

  patchedPackagesDirect = patchedPackages;
  newPackages = selectDerivations self.packageSets.pkgs;

  patchedPackagesOverlay = pipe [ ./pkgs/overlay.nix ./patches/overlay.nix ] [
    (map import)
    pkgs.appendOverlays
    (flattenAttrsFromPaths (packagePaths patchedPackages))
    recurseIntoAttrs
  ];
  patchedPackages = selectDerivations self.packageSets.patches;
}

M default.nix => default.nix +24 -11
@@ 1,14 1,27 @@
{ pkgs ? import <nixpkgs> { } }:

with import ./lib/extension.nix { inherit (pkgs) lib; };
let
  newPkgs = import ./pkgs { inherit pkgs; };
  patchedPkgs = import ./patches { pkgs = pkgs // newPkgs; };
  aasgPkgs = newPkgs // patchedPkgs;
in
{
  lib = import ./lib { inherit (pkgs) lib; };
  modules = import ./modules;
  overlays = {
    pkgs = import ./pkgs/overlay.nix;
    patches = import ./patches/overlay.nix;
  overlayToPackageSet = overlays: manifest:
    pipe overlays [
      pkgs.appendOverlays
      (copyAttrsByPath manifest)
      recurseIntoAttrsRecursive
    ];
  self = {
    lib = import ./lib { inherit (pkgs) lib; };
    modules = import ./modules;
    overlays = {
      pkgs = import ./pkgs/overlay.nix;
      patches = import ./patches/overlay.nix;
    };
    packageSets = {
      pkgs = overlayToPackageSet [ self.overlays.pkgs ] (import ./pkgs/manifest.nix);
      patches = overlayToPackageSet [ self.overlays.pkgs self.overlays.patches ] (import ./patches/manifest.nix);
    };
  };
} // aasgPkgs
in
foldl' recursiveUpdate self [
  self.packageSets.pkgs
  self.packageSets.patches
]

M flake.nix => flake.nix +4 -2
@@ 9,12 9,14 @@
      inherit (flake-utils.lib) defaultSystems flattenTree;
      inherit (nixpkgs.lib.attrsets) filterAttrs genAttrs mapAttrs;
      inherit (nixpkgs.lib.strings) hasPrefix hasSuffix;
      inherit (nixpkgs.lib.trivial) id pipe;
      inherit (nixpkgs.lib.trivial) id flip pipe;
    in
    {
      #lib = import ./lib { inherit (nixpkgs) lib; };
      packages = genAttrs defaultSystems
        (system: pipe (import ./pkgs { pkgs = nixpkgs.legacyPackages.${system}; }) [
        (system: pipe (import ./. { pkgs = nixpkgs.legacyPackages.${system}; }) [
          # Remove non–package attributes.
          (flip builtins.removeAttrs [ "lib" "modules" "overlays" "packageSets" ])
          # Filter out linuxPackages before it gets evaluated.
          (if ! hasSuffix "linux" system
          then filterAttrs (attr: drv: ! hasPrefix "linuxPackages" attr)

M lib/attrsets.nix => lib/attrsets.nix +27 -0
@@ 1,6 1,7 @@
{ lib, aasgLib }:
let
  inherit (builtins) attrNames concatStringsSep intersectAttrs isAttrs;
  inherit (lib) isDerivation;
in
rec {
  /*


@@ 11,6 12,32 @@ rec {
  capitalizeAttrNames = /*attrs:*/
    lib.mapAttrs' (name: value: lib.nameValuePair (aasgLib.capitalize name) value);

  /* copyAttrsByPath :: [[string] -> set -> set
   *
   * Recreate attrs recursively with only the attributes listed in
   * paths.
   */
  copyAttrsByPath = paths: attrs:
    builtins.foldl' updateNewRecursive { }
      (map (path: lib.setAttrByPath path (lib.getAttrFromPath path attrs)) paths);

  /* recurseIntoAttrs :: set -> set
   *
   * Polyfill of nixpkgs.recurseIntoAttrs for when it's not available
   * under lib.
   */
  recurseIntoAttrs = lib.recurseIntoAttrs or (attrs: attrs // { recurseForDerivations = true; });

  /* recurseIntoAttrsRecursive :: set -> set
   *
   * Recursive variant of recurseIntoAttrs, mark attrs as containing
   * derivations recursively until a derivation or non-attrset value
   * is reached.
   */
  recurseIntoAttrsRecursive = attrs:
    if isAttrs attrs && ! isDerivation attrs
    then recurseIntoAttrs (lib.mapAttrs (_: recurseIntoAttrsRecursive) attrs)
    else attrs;

  /*
   * Like the update operator `//`, but throws if the right-hand

M lib/default.nix => lib/default.nix +6 -1
@@ 3,7 3,12 @@ let
  callLibs = file: import file { inherit lib; aasgLib = self; };
  self = rec {
    attrsets = callLibs ./attrsets.nix;
    inherit (attrsets) capitalizeAttrNames updateNew updateNewRecursive;
    inherit (attrsets)
      capitalizeAttrNames
      copyAttrsByPath
      recurseIntoAttrsRecursive
      updateNew
      updateNewRecursive;

    declarativeEnvironments = callLibs ./declarative-env.nix;
    inherit (declarativeEnvironments) declareEnvironment baseEnvironment;

D patches/default.nix => patches/default.nix +0 -13
@@ 1,13 0,0 @@
{ pkgs }:
let
  # self is not included here to avoid recursion.
  callPackage = pkgs.lib.callPackageWith pkgs;
  self = rec {

    haunt = callPackage ./haunt { };

    ipfs-cluster = callPackage ./ipfs-cluster { };

  };
in
self

A patches/manifest.nix => patches/manifest.nix +4 -0
@@ 0,0 1,4 @@
[
  [ "haunt" ]
  [ "ipfs-cluster" ]
]

M patches/overlay.nix => patches/overlay.nix +2 -2
@@ 2,8 2,8 @@ final: prev:

{

  haunt = import ./haunt { inherit (prev) haunt; };
  haunt = final.callPackage ./haunt { inherit (prev) haunt; };

  ipfs-cluster = import ./ipfs-cluster { inherit (prev) ipfs-cluster; };
  ipfs-cluster = final.callPackage ./ipfs-cluster { inherit (prev) ipfs-cluster; };

}

D pkgs/default.nix => pkgs/default.nix +0 -50
@@ 1,50 0,0 @@
{ pkgs }:
let
  callPackage = pkgs.lib.callPackageWith (pkgs // self);
  callPackageWithMerged = attrName: f: extraArgs:
    let
      mergedSubset = pkgs.${attrName} // self.${attrName};
      subsetArgs = builtins.listToAttrs [{ name = attrName; value = mergedSubset; }];
    in
    callPackage f (subsetArgs // extraArgs);
  self = rec {

    dma = callPackage ./tools/networking/dma { };

    drep = callPackage ./tools/text/drep { };

    dyndnsc = callPackageWithMerged "python3Packages" ./tools/networking/dyndnsc { };

    esbuild = callPackage ./development/tools/esbuild { };

    guile-commonmark = callPackage ./development/guile-modules/guile-commonmark { };

    guile-json = callPackage ./development/guile-modules/guile-json { };

    haunt = callPackage ./applications/misc/haunt { };

    iwgtk = callPackage ./applications/networking/iwgtk { };

    linuxPackages = pkgs.recurseIntoAttrs (linuxPackagesFor pkgs.linux);
    linuxPackages_latest = pkgs.recurseIntoAttrs (linuxPackagesFor pkgs.linux_latest);
    linuxPackagesFor = kernel:
      let
        ksuper = pkgs.linuxPackagesFor kernel;
        kself = import ./os-specific/linux/kernel-packages.nix (ksuper // kself) ksuper;
      in
      pkgs.lib.makeExtensible (_: kself);

    python3Packages =
      let
        pysuper = pkgs.python3Packages;
        pyself = import ./development/python-modules (pysuper // pyself) pysuper;
      in
      pkgs.recurseIntoAttrs pyself;

    shellharden = callPackage ./development/tools/shellharden { };

    trust-dns = callPackage ./servers/dns/trust-dns { };

  };
in
self

M pkgs/development/python-modules/default.nix => pkgs/development/python-modules/default.nix +2 -2
@@ 1,5 1,5 @@
pyself: pysuper:
final: prev:

{
  daemonocle = pysuper.callPackage ./daemonocle { };
  daemonocle = final.callPackage ./daemonocle { };
}

A pkgs/manifest.nix => pkgs/manifest.nix +15 -0
@@ 0,0 1,15 @@
[
  [ "dma" ]
  [ "drep" ]
  [ "dyndnsc" ]
  [ "esbuild" ]
  [ "guile-commonmark" ]
  [ "guile-json" ]
  [ "haunt" ]
  [ "iwgtk" ]
  [ "linuxPackages" "rtl8723bu" ]
  [ "linuxPackages_latest" "rtl8723bu" ]
  [ "python3Packages" "daemonocle" ]
  [ "shellharden" ]
  [ "trust-dns" ]
]

M pkgs/os-specific/linux/kernel-packages.nix => pkgs/os-specific/linux/kernel-packages.nix +2 -2
@@ 1,5 1,5 @@
kself: ksuper:
final: prev:

{
  rtl8723bu = ksuper.callPackage ./rtl8723bu { };
  rtl8723bu = final.callPackage ./rtl8723bu { };
}

M pkgs/overlay.nix => pkgs/overlay.nix +11 -11
@@ 2,29 2,29 @@ final: prev:

{

  dma = prev.callPackage ./tools/networking/dma { };
  dma = final.callPackage ./tools/networking/dma { };

  drep = prev.callPackage ./tools/text/drep { };
  drep = final.callPackage ./tools/text/drep { };

  dyndnsc = prev.callPackage ./tools/networking/dyndnsc { };
  dyndnsc = final.callPackage ./tools/networking/dyndnsc { };

  esbuild = prev.callPackage ./development/tools/esbuild { };
  esbuild = final.callPackage ./development/tools/esbuild { };

  guile-commonmark = prev.callPackage ./development/guile-modules/guile-commonmark { };
  guile-commonmark = final.callPackage ./development/guile-modules/guile-commonmark { };

  guile-json = prev.callPackage ./development/guile-modules/guile-json { };
  guile-json = final.callPackage ./development/guile-modules/guile-json { };

  haunt = prev.callPackage ./applications/misc/haunt { };
  haunt = final.callPackage ./applications/misc/haunt { };

  iwgtk = prev.callPackage ./applications/networking/iwgtk { };
  iwgtk = final.callPackage ./applications/networking/iwgtk { };

  linuxPackagesFor = kernel:
    (prev.linuxPackagesFor kernel).extend (import ./os-specific/linux/kernel-packages.nix);

  python3 = prev.python3.override { packageOverrides = import ./development/python-modules; };
  python3 = prev.python3.override { packageOverrides = import ./development/python-modules; self = final.python3; };

  shellharden = prev.callPackage ./development/tools/shellharden { };
  shellharden = final.callPackage ./development/tools/shellharden { };

  trust-dns = prev.callPackage ./servers/dns/trust-dns { };
  trust-dns = final.callPackage ./servers/dns/trust-dns { };

}