~aasg/nixexprs

cc4d051fd51eba55be7d9a65c0a7c023677c2b71 — Aluísio Augusto Silva Gonçalves 6 months ago 7df1257
lib: Fully separate from Nixpkgs' lib

This is sort of a revert and different take on 2703028a92be, which I
decided to do after reading github:NixOS/nixpkgs#51797.  Preventing
our functions from being transparently accessed by derivations and
modules makes it easier to figure out what else needs to be upstreamed
with them.  Where that is still wanted, the new `lib/extension.nix` is
an overlay done right, balking at conflicts between Nixpkgs and us.
M default.nix => default.nix +5 -7
@@ 1,16 1,14 @@
{ pkgs ? import <nixpkgs> { } }:
let
  pkgsWithLib = pkgs.extend (import ./lib/overlay.nix);
  newPkgs = import ./pkgs { pkgs = pkgsWithLib; };
  patchedPkgs = import ./patches { pkgs = pkgsWithLib // newPkgs; };
  myPkgs = newPkgs // patchedPkgs;
  newPkgs = import ./pkgs { inherit pkgs; };
  patchedPkgs = import ./patches { pkgs = pkgs // newPkgs; };
  aasgPkgs = newPkgs // patchedPkgs;
in
{
  lib = import ./lib pkgs.lib;
  lib = import ./lib { inherit pkgs; };
  modules = import ./modules;
  overlays = {
    lib = import ./lib/overlay.nix;
    pkgs = import ./pkgs/overlay.nix;
    patches = import ./patches/overlay.nix;
  };
} // myPkgs
} // aasgPkgs

A lib/attrsets.nix => lib/attrsets.nix +35 -0
@@ 0,0 1,35 @@
{ lib, ... }:
let
  inherit (builtins) attrNames concatStringsSep intersectAttrs isAttrs;
in
rec {
  /*
   * Like the update operator `//`, but throws if the right-hand
   * attrset contains an attribute that already exists in the
   * left-hand side.
   *
   * See `lib.attrsets.overrideExisting` for the opposite behavior.
   */
  updateNew = into: new:
    let
      commonAttributes = attrNames (intersectAttrs into new);
    in
    if commonAttributes == [ ]
    then into // new
    else throw "attrsets have the following attributes in common: ${concatStringsSep ", " commonAttributes}";

  /*
   * Recursive variant of updateNew.
   */
  updateNewRecursive = into: new:
    let
      commonAttributes = attrNames (intersectAttrs into new);
      commonNonAttrsets = builtins.filter (name: ! (isAttrs into.${name} && isAttrs new.${name})) commonAttributes;
      mergedCommonAttrsets = builtins.listToAttrs
        (map (name: lib.nameValuePair name (updateNewRecursive into.${name} new.${name})) commonAttributes);
    in
    if commonNonAttrsets == [ ]
    then into // new // mergedCommonAttrsets
    else
      throw "attrsets have the following attributes in common: ${concatStringsSep ", " commonNonAttrsets}";
}

M lib/declarative-env.nix => lib/declarative-env.nix +2 -2
@@ 1,9 1,9 @@
{ ... }:
{ lib }:

# Based on https://gist.github.com/lheckemann/402e61e8e53f136f239ecd8c17ab1deb
{ file # Path to the environment definition file
, withPkgs # Function returning the packages to install
, name ? pkgs.lib.removeSuffix ".nix" (baseNameOf file) # Name of the environment
, name ? lib.removeSuffix ".nix" (baseNameOf file) # Name of the environment
, drvName ? "${name}-environment" # Name of the generated derivaqtion
, profile ? "/nix/var/nix/profiles/${name}" # Path to the profile
, pkgs ? import <nixpkgs> { } # Nixpkgs

M lib/default.nix => lib/default.nix +7 -3
@@ 1,8 1,12 @@
super:
{ pkgs }:
let
  callLibs = file: import file { lib = super // self; };
  lib = pkgs.lib;
  callLibs = file: import file { lib = lib; aasgLib = self; };
  self = rec {
    declareEnvironment = super.makeOverridable (callLibs ./declarative-env.nix);
    attrsets = callLibs ./attrsets.nix;
    inherit (attrsets) updateNew updateNewRecursive;

    declareEnvironment = lib.makeOverridable (callLibs ./declarative-env.nix);

    lists = callLibs ./lists.nix;
    inherit (lists) indexOf isSubsetOf;

A lib/extension.nix => lib/extension.nix +9 -0
@@ 0,0 1,9 @@
{ pkgs }:
/*
 * Merge of the Nixpkgs and aasg libs.
 */
let
  lib = pkgs.lib;
  aasgLib = import ./. { inherit pkgs; };
in
aasgLib.updateNewRecursive lib aasgLib

D lib/overlay.nix => lib/overlay.nix +0 -3
@@ 1,3 0,0 @@
final: prev: {
  lib = prev.lib // (import ./. prev.lib);
}

M modules/programs/dma.nix => modules/programs/dma.nix +1 -3
@@ 1,8 1,6 @@
{ config, lib, pkgs, ... }:
with lib;
with import ../../lib/extension.nix { inherit pkgs; };
let
  inherit (pkgs.lib) indexOf;

  cfg = config.programs.dma;

  package = pkgs.dma or (import ../../pkgs/tools/networking/dma { inherit pkgs; });

M modules/services/networking/matrix-appservice-irc.nix => modules/services/networking/matrix-appservice-irc.nix +1 -3
@@ 1,8 1,6 @@
{ config, lib, pkgs, ... }:
with lib;
with import ../../../lib/extension.nix { inherit pkgs; };
let
  inherit (pkgs.lib) isSubsetOf;

  format = pkgs.formats.yaml { };
  cfg = config.services.matrix-appservice-irc;
  pkg = cfg.package;