1# tops and bottoms
2this document outlines things that i learned from various sources and some pure guesswork
3
4> To learn Nix is to learn to suffer, and to learn the way of numbing the pain
5>
6> — Cassie circa. 2023
7
8## overriding packages
9the pill confused me and i thought i had to make overlays to do overrides but no
10
11in packages (i.e. `environment.systemPackages`), just do
12```nix
13#-------8<-Snip-------
14environment = {
15 systemPackages = with pkgs; [
16 (discord.override {withOpenASAR = true;})
17 ()
18 ];
19};
20#-------Snip->8-------
21```
22
23This works as well
24```nix
25security.sudo.package = (pkgs.sudo.override {withInsults = true;});
26```
27
28## overlays
29overlays are useful when you want to refer to a package globally.
30
31the gist of overlays is as thus:
32
33> overlay is just `final: pref: {}` functions where dumbed down idea is
34> that you do pkg = prev.pkg.override and refer to everything else from
35> final
36>
37> idea is like final = prev // overlay(prev, final)
38> (it's a recursive definition)
39
40(poorly made) example overlays can be found [here](https://github.com/soopyc/nixos-config/blob/master/overlays/discord-canary.nix)
41
42currently in-use and slightly better overlays can be found in this repo! head over to /overlays to see them.
43
44*note: replace `self: super:` with `final: prev:` for consistency*
45
46*concept and content by \@natsukagami*
47
48## extra opts
49a way of passing additional options globally to modules is by using extraOpts.
50
51in nix flakes, this is accomplished by using `specialArgs` in `nixosSystem`.
52
53for example, check out this line in our flake.nix:
54
55https://github.com/soopyc/nix-on-koumakan/blob/492dfaa01808c2aa5dbb2d8223163e92bcef673b/flake.nix#L22-L28
56
57this avoids the horror of `import ../../../utils/bar.nix;`
58
59refer to [nixpkgs:nixos/lib/eval-config.nix] and [nixpkgs:lib/modules.nix#122] for more info
60
61*pointers by \@natsukagami*
62
63## @ (at) syntax
64very simple.
65
66```nix
67args@{a, b, c, ...}: {
68 # args.a and a are the same
69 some = "value";
70}
71```
72
73## nginx regex location
74```nix
75{
76 locations."~ \.php$".extraConfig = ''
77 # balls
78 '';
79}
80```
81from [nixos wiki](https://nixos.wiki/wiki/Nginx#LEMP_stack)
82
83## adding a package with an overlay to a package set
84
85for package sets with a scope, you will have to do something like
86```nix
87final: prev: {
88 nimPackages = prev.nimPackages.overrideScope (final': prev': {
89 sha1 = final'.callPackage ./sha1.nix {};
90 oauth = final'.callPackage ./oauth.nix {};
91 });
92}
93```
94There's an alternative method that i used to use here:
95
96https://github.com/soopyc/nix-on-koumakan/blob/30e65402d22b000a3b5af6c9e5ea48a2b58a54e0/overlays/nim/oauth/default.nix
97
98however i do not think that's the best way lol
99
100# Common pitfalls
101## importing nixpkgs with an empty attrset
102
103ever had this in your flake.nix
104
105```nix
106{
107 outputs = { nixpkgs, ... }@inputs: let
108 pkgs = import nixpkgs {};
109 lib = nixpkgs.lib;
110 in {
111 # ...
112 };
113}
114```
115
116... and got fucked with this?
117```shell
118error:
119 … while checking flake output 'nixosConfigurations'
120
121 at /nix/store/lz2ra1180qfffmpwg41jpyg1z602qdgx-source/flake.nix:50:5:
122
123 49| in {
124 50| nixosConfigurations = {
125 | ^
126 51| koumakan = (import ./systems/koumakan { inherit pkgs lib inputs; });
127
128 … while checking the NixOS configuration 'nixosConfigurations.koumakan'
129
130 at /nix/store/lz2ra1180qfffmpwg41jpyg1z602qdgx-source/flake.nix:51:7:
131
132 50| nixosConfigurations = {
133 51| koumakan = (import ./systems/koumakan { inherit pkgs lib inputs; });
134 | ^
135 52| };
136
137 (stack trace truncated; use '--show-trace' to show the full trace)
138
139 error: attribute 'currentSystem' missing
140
141 at /nix/store/5c0k827yjq7j24qaq8l2fcnsxp7nv8v1-source/pkgs/top-level/impure.nix:17:43:
142
143 16| # (build, in GNU Autotools parlance) platform.
144 17| localSystem ? { system = args.system or builtins.currentSystem; }
145 | ^
146 18|
147```
148
149just don't!!!11 remove the pkgs definition. (note that this only applies to `pkgs = import nixpkgs {};`)
150
151explanation
152
153> you shouldn't ever really import nixpkgs with an empty attrset either
154>
155> that causes it to fall back on guessing your system using `builtins.currentSystem`,
156> which is impure and so not allowed in pure evaluation mode
157>
158> — \@getchoo
159
160# Useful links
161
162Builtin stdlib functions search engine: https://noogle.dev/
163
164
165<!--links-->
166[nixpkgs:lib/modules.nix#122]: https://github.com/NixOS/nixpkgs/blob/6e68daefde56a7a8e6fe7c3ca9ceeb436294bb9f/lib/modules.nix#L122
167[nixpkgs:nixos/lib/eval-config.nix]: https://github.com/NixOS/nixpkgs/blob/5054472759a3b0df8e18cfe4031a5eff92d4cdc3/nixos/lib/eval-config.nix