~aasg/nixexprs

2703028a92be7350ff7f6cb39cc64c3e45859420 — Aluísio Augusto Silva Gonçalves 6 months ago f70d7dd
Make our lib functions available to ourselves

It's a weird song and dance I'm not confident in, and the overlaid
functions aren't available to modules in the `lib` argument (we have
to get them individually from `pkgs.lib` instead to avoid infinite
recursion).

I should revisit this sometime.  Maybe.
M default.nix => default.nix +7 -4
@@ 1,13 1,16 @@
{ pkgs ? import <nixpkgs> { } }:
let
  myPkgs = import ./pkgs { inherit pkgs; };
  myPatchedPkgs = import ./patches { pkgs = pkgs // myPkgs; };
  pkgsWithLib = pkgs.extend (import ./lib/overlay.nix);
  newPkgs = import ./pkgs { pkgs = pkgsWithLib; };
  patchedPkgs = import ./patches { pkgs = pkgsWithLib // newPkgs; };
  myPkgs = newPkgs // patchedPkgs;
in
{
  lib = import ./lib { inherit (pkgs) lib; };
  lib = import ./lib pkgs.lib;
  modules = import ./modules;
  overlays = {
    lib = import ./lib/overlay.nix;
    pkgs = import ./pkgs/overlay.nix;
    patches = import ./patches/overlay.nix;
  };
} // myPkgs // myPatchedPkgs
} // myPkgs

M lib/default.nix => lib/default.nix +9 -30
@@ 1,30 1,9 @@
{ lib }:

{
  /* Return the index of the first element equal to `x` in the list
   * `xs`.
   *
   * Example:
   *     indexOf "c" ["a b c d e"]
   *     => 2
   *
   *     indexOf "g" ["a b c d e"]
   *     => -1
   */
  indexOf = x: xs:
    let indexOfRec = i: xs:
      if xs == [ ] then -1
      else if (builtins.head xs) == x then i
      else indexOfRec (i + 1) (builtins.tail xs);
    in indexOfRec 0 xs;

  /* Test if $ys ⊆ xs$, that is, if every element of the list `ys` is
   * also in `xs`.
   *
   * Example:
   *     isSubsetOf [1 2 3 4 5] [1 3 5]
   *     => true
   */
  isSubsetOf = xs: ys:
    builtins.all (y: builtins.elem y xs) ys;
}
super:
let
  callLibs = file: import file { lib = super // self; };
  self = rec {
    lists = callLibs ./lists.nix;
    inherit (lists) indexOf isSubsetOf;
  };
in
self

A lib/lists.nix => lib/lists.nix +30 -0
@@ 0,0 1,30 @@
{ ... }:

rec {
  /* Return the index of the first element equal to `x` in the list
   * `xs`.
   *
   * Example:
   *     indexOf "c" ["a b c d e"]
   *     => 2
   *
   *     indexOf "g" ["a b c d e"]
   *     => -1
   */
  indexOf = x: xs:
    let indexOfRec = i: xs:
      if xs == [ ] then -1
      else if (builtins.head xs) == x then i
      else indexOfRec (i + 1) (builtins.tail xs);
    in indexOfRec 0 xs;

  /* Test if $ys ⊆ xs$, that is, if every element of the list `ys` is
   * also in `xs`.
   *
   * Example:
   *     isSubsetOf [1 2 3 4 5] [1 3 5]
   *     => true
   */
  isSubsetOf = xs: ys:
    builtins.all (y: builtins.elem y xs) ys;
}

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

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

  cfg = config.programs.dma;



@@ 17,7 17,7 @@ let
  tlsConfigSettings = [ false "require-tls" "require-starttls" "allow-starttls" ];
  tlsConfigValues = [ null "SECURETRANSFER" "STARTTLS" "OPPORTUNISTIC_TLS" ];
  tlsConfig = secureTransfer:
    concatStringsSep "\n" (sublist 1 (myLib.indexOf secureTransfer tlsConfigSettings) tlsConfigValues);
    concatStringsSep "\n" (sublist 1 (indexOf secureTransfer tlsConfigSettings) tlsConfigValues);

  masqueradeConfig = masquerade:
    if (masquerade.user != null) && (masquerade.domain != null) then

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

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


@@ 77,7 77,7 @@ in
    assertions =
      [
        {
          assertion = myLib.isSubsetOf (builtins.attrNames cfg.settings.ircService.servers) (builtins.attrNames cfg.botPasswordFiles);
          assertion = isSubsetOf (builtins.attrNames cfg.settings.ircService.servers) (builtins.attrNames cfg.botPasswordFiles);
          message = "botPasswordFiles references servers not present in the settings";
        }
      ];