ChrisTitusTech / nixos-titus

Titus's personal NIX OS Configuration

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

you have loads of redundant packages in environment.systemPackages

miki223 opened this issue · comments

let me explain

Nix is a functional domain specific programming language mainly designed for building packages(derivations in Nix terms)
NixOS modules like Nix packages are functions that take a single input & return a single output
for packages the single input is an attribute set(like a hash map in other languages) of dependencies(other nix expressions/packages) the output is usually just the outpit of the stdenv.mkDerivation function
which is a json file that defines the build environment & build commands of a package which Nix then takes interprets & executes
to create files in the Nix store

here comes the difference between packages & Nix OS modules
most Nix OS modules don't call stdenv.mkDerivation directly they simply return an attribute set containing 2 attribute sets options & config options govern the behavior of the module that declared them while config contains the options declared by other modules which we can change if 2 modules try to change the same option the module system tries to merge the 2
once all of that is done only then the module system ends up calling the stdenv.mkderivation to produce the derivation that when executed(realised in Nix terms) produces the final system & a bash script called activate to finally symlink the final system into place & does other things if required btw all the system generations are symlinked to /nix/var/nix/profiles/system-N-link N being a number for the generation when you garbage collect the unused generations get wiped of course
the current system is also symlinked under /run/current-system/ the generation you booted from is /run/booted-system the booted system is required for it's kernel & modules so it you switch the current system & garbage collect both it & the booted system must stick around a side benefit of this is that you can run sudo /run/booted-system/bin/activate switch to switch back to it if everything breaks & dies on the current system
the current system will become the booted system when it's booted from the bootloader

but back to modules by this point if you were crazy enough to read this far you probably realized that configuration.nix is itself just a regular old module it just doesn't export any options of it's own & thus the module system is free to assume that when you set the value of environment.systemPackages you mean config.environment.systemPackages
herels the redundant part of the configuration.nix in this repo it adds stuff like flatpak to the system packages this is redundant & cluttered because the flatpak.nix module does it for flatpak when services.flatpak.enable is set to true xserver.nix does it for Xorg etc there is no reason to duplicate the work done by other modules in confitguration.nix

lists & attribute sets can be merged but bools & other simple types cannot so if
for instance if a module sets boot.loader.grub.enable = false & you try to set it to true in /etc/nixos/configuration.nix
will cause an error
i mention this is because this is what happens with the ISO generation functions Nix OS ISOs as well as a lot of other ISOs use syslinux & thus the modules disable grub

now you may be asking is there a way to force a value & avoid this merge conflict
well yes
here the inputs to /etc/nixos/configuration.nix & other modules
the input attribute set usually contains lib pkgs & config
lib is the attribute set of Nix utility functions one such function is mkForce
so to force a specific value for an option you can for instance environment.systemPackages = with pkgs; lib.mkForce [PACKAGES]; there is no reason ti do this & doing it would make this package list the final one
no more merges with the values of this option from other modules
even with how many redundant packages chris added here forcing this to be the final list might still break the system because some important packages are probably still missing from the list here & are added by other modules this would probably be the easiest way to break the system you can still roll back though
but nixos-rebuild would be missing so you would need to run the activate script directly
those scripts define their own environments PATHs etc that do not reference the system paths but the actual nix store paths which are still usable even if they aren't symlinked to the current system or anywhere for that matter