nix/nixos/nix.nix
2025-04-05 16:34:08 -05:00

138 lines
4.4 KiB
Nix

{
config,
host,
inputs,
lib,
pkgs,
self,
user,
dots,
...
}:
{
# execute shebangs that assume hardcoded shell paths
services.envfs.enable = true;
# run unpatched binaries on nixos
programs.nix-ld.enable = true;
environment = {
# for nixlang / nixpkgs
systemPackages = with pkgs; [
nix-init
nix-update
nixfmt-rfc-style
];
};
# make a symlink of flake within the generation (e.g. /run/current-system/src)
system.extraSystemBuilderCmds = "ln -s ${self.sourceInfo.outPath} $out/src";
systemd.tmpfiles.rules = [
# cleanup nixpkgs-review cache on boot
"D! ${config.hm.xdg.cacheHome}/nixpkgs-review 1755 ${user} users 1d"
# cleanup channels so nix stops complaining
"D! /nix/var/nix/profiles/per-user/root 1755 root root 1d"
];
custom.shell.packages = {
# list all installed packages
nix-list-packages = {
text =
let
allPkgs = map (p: p.name) (
config.environment.systemPackages ++ config.users.users.${user}.packages ++ config.hm.home.packages
);
in
''sort -ui <<< "${lib.concatLines allPkgs}"'';
};
};
nix =
let
flakeInputs = lib.filterAttrs (_: lib.isType "flake") inputs;
nixPath = lib.mapAttrsToList (n: _: "${n}=flake:${n}") flakeInputs;
in
{
inherit nixPath;
channel.enable = false;
# required for nix-shell -p to work
registry = lib.mapAttrs (_: flake: { inherit flake; }) flakeInputs;
# gc = {
# # Automatic garbage collection
# automatic = true;
# dates = "daily";
# options = "--delete-older-than 7d";
# };
package = pkgs.nixVersions.latest;
settings = {
auto-optimise-store = true; # Optimise symlinks
# re-evaluate on every rebuild instead of "cached failure of attribute" error
# eval-cache = false;
# required to be set, for some reason nix.nixPath does not write to nix.conf
nix-path = nixPath;
warn-dirty = false;
# removes ~/.nix-profile and ~/.nix-defexpr
use-xdg-base-directories = true;
# use flakes
experimental-features = [
"nix-command"
"flakes"
];
substituters = [
"https://hyprland.cachix.org"
"https://nix-community.cachix.org"
"https://ghostty.cachix.org"
"https://rayandrew.cachix.org"
];
# allow building and pushing of laptop config from desktop
trusted-users = [ user ];
trusted-public-keys = [
"hyprland.cachix.org-1:a7pgxzMz7+chwVL3/pzj6jIBMioiJM7ypFP8PwtkuGc="
"nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs="
"ghostty.cachix.org-1:QB389yTa6gTyneehvqG58y0WnHjQOqgnA+wBnpWWxns="
"rayandrew.cachix.org-1:kJnvdWgUyErPGaQWgh/yyu91szgRYD+V/WQ4Dbc4n9M="
];
};
# // lib.optionalAttrs (config.nix.package.pname == "lix") {
# repl-overlays = [ ./repl-overlays.nix ];
# };
};
# better nixos generation label
# https://reddit.com/r/NixOS/comments/16t2njf/small_trick_for_people_using_nixos_with_flakes/k2d0sxx/
system.nixos.label = lib.concatStringsSep "-" (
(lib.sort (x: y: x < y) config.system.nixos.tags)
++ [ "${config.system.nixos.version}.${self.sourceInfo.shortRev or "dirty"}" ]
);
# add nixos-option workaround for flakes
# https://github.com/NixOS/nixpkgs/issues/97855#issuecomment-1075818028
nixpkgs.overlays = [
(_: prev: {
nixos-option =
let
flake-compat = prev.fetchFromGitHub {
owner = "edolstra";
repo = "flake-compat";
rev = "12c64ca55c1014cdc1b16ed5a804aa8576601ff2";
hash = "sha256-hY8g6H2KFL8ownSiFeMOjwPC8P0ueXpCVEbxgda3pko=";
};
prefix = ''(import ${flake-compat} { src = ${dots}; }).defaultNix.nixosConfigurations.${host}'';
in
prev.runCommandNoCC "nixos-option" { buildInputs = [ prev.makeWrapper ]; } ''
makeWrapper ${lib.getExe prev.nixos-option} $out/bin/nixos-option \
--add-flags --config_expr \
--add-flags "\"${prefix}.config\"" \
--add-flags --options_expr \
--add-flags "\"${prefix}.options\""
'';
})
];
# enable man-db cache for fish to be able to find manpages
# https://discourse.nixos.org/t/fish-shell-and-manual-page-completion-nixos-home-manager/15661
documentation.man.generateCaches = true;
}