os/lib
bors[bot] 3c772df5b0
Merge #215
215: lib/pathsToImportedAttrs: discard string context in the names to prevent flake check error r=nrdxp a=Pacman99

So I learned about this myself while working on it. Nix has a thing called string context, where strings refer to various store paths that they used to contain. This is normally a good thing to guarantee purity, but flake outputs cannot contain store paths. So when `pathsToImportedAttrs` is passed a list of store paths, and is then used for a flake output, we get a very confusing error that the string refers to a store path - even though it doesn't look like it does! 
I think its good to review this as a separate Pr, since it requires calling an unsafe nix builtin.

Co-authored-by: Pacman99 <pachum99@gmail.com>
2021-03-26 21:30:04 +00:00
..
devos Merge #211 2021-03-26 21:22:49 +00:00
attrs.nix lib/pathsToImportedAttrs: discard string context in the names to prevent flake check error 2021-03-26 08:10:56 -07:00
default.nix hosts: set nixpkgs.pkgs based on nixpkgs.system 2021-03-19 12:23:23 -07:00
lists.nix lib/pathsIn: don't error if path doesn't exist 2021-03-25 15:21:10 -07:00
README.md lib: add usage docs 2021-03-01 14:37:43 -05:00
strings.nix lib: add rgxToString function 2021-03-14 21:40:49 -06:00

Lib

The lib directory mirrors the upstream concepts of nixpkgs:./lib, nixpkgs:./nixos/lib and nixpkgs:./pkgs/pkgs-lib, but also occasionally nixpkgs:./pkgs/build-support.

It comes with functions necesary to declutter devos itself, but you are welcome to extend it to your needs.

For example:

  • you want to add a library function that depends on some packages and use it throughout your devos environment: place it into ./lib as if you would place it into nixpkgs:./pkgs/pkgs-lib.

  • you want to add library functions that don't depend on pkgs: place them into ./lib as if you would place them into nixpkgs:./lib.

  • need to try out a newish custom build support: place it here before upstreaming into nixpkgs:./pkgs/build-support.

  • you want to reutilize certain module configuration functions or helpers: place them into ./lib as if you would place them into nixpkgs:./nixos/lib.

Once your library grows, we recoomend you start organizing them into subfolders analogous nixpkgs:

nixpkgs devos
./lib ./lib
./pkgs/pkgs-lib ./lib/pkgs-lib
./nixos/lib ./lib/nixos-lib
./pkgs/build-support ./lib/pkgs-build

Example

lib/nixos-lib/mkCustomI3BindSym/default.nix:

{ pkgs, writers, ... }:
{ name, cmd, workspace, baseKey }:
let
  isWorkspaceEmpty = writers.writePython3 "is-workspace-empty" {
    libraries = [ pkgs.python3Packages.i3ipc ];
  } (builtins.readFile ./is-workspace-empty.py);

  ws = builtins.toString workspace;
in
''

  # ${name}
  #bindsym ${baseKey}+${ws} workspace ${ws}; exec ${cmd}
  bindsym ${baseKey}+${ws} workspace ${ws}; exec bash -c "${isWorkspaceEmpty} && ${cmd}"
''

lib/nixos-lib/mkCustomI3BindSym/is-workspace-empty.py:

# returns 0/1 if current workspace is empty/non-empty

import i3ipc

i3 = i3ipc.Connection()
tree = i3.get_tree()


def current_workspace():
    return tree.find_focused().workspace()


if current_workspace().leaves():
    print("Error current workspace is not empty")
    exit(1)
exit(0)

lib/default.nix:

{ nixos, pkgs, ... }:
# ...
{
  # ...
  mkCustomI3BindSym = pkgs.callPackage ./nixos-lib/mkCustomI3BindSym { };
}