nixos: refactor to auto-init modules

This commit is contained in:
Harsh Shandilya 2023-12-18 13:17:52 +05:30
parent e8610df232
commit d0a0680218
42 changed files with 129 additions and 331 deletions

View File

@ -1,4 +1,9 @@
_: {
targets.genericLinux.enable = false;
xdg.enable = false;
xdg.mime.enable = false;
services.gpg-agent.enable = false;
services.git-sync.enable = false;
programs.bash = {
initExtra = ''
# Load completions from system

275
flake.nix
View File

@ -48,215 +48,120 @@
inputs.sops-nix.inputs.nixpkgs-stable.follows = "";
outputs = {
self,
nixpkgs,
home-manager,
darwin,
self,
deploy-rs,
systems,
...
} @ inputs: let
forAllSystems = nixpkgs.lib.genAttrs (import systems);
packagesFn = system:
import nixpkgs {
inherit system;
findModules = dir:
builtins.concatLists (builtins.attrValues (builtins.mapAttrs
(name: type:
if type == "regular"
then [
{
name = builtins.elemAt (builtins.match "(.*)\\.nix" name) 0;
value = dir + "/${name}";
}
]
else if
(builtins.readDir (dir + "/${name}"))
? "default.nix"
then [
{
inherit name;
value = dir + "/${name}";
}
]
else findModules (dir + "/${name}")) (builtins.readDir dir)));
pkgsFor = system:
import inputs.nixpkgs {
config = {
allowUnfree = true;
};
overlays = [
self.overlay
inputs.custom-nixpkgs.overlays.default
inputs.nixgl.overlays.default
inputs.nix-vscode-extensions.overlays.default
(import ./nixos/overlays)
];
localSystem = {inherit system;};
};
pkgs = forAllSystems packagesFn;
hmModules = [
./nixos/modules/home-manager
./nixos/modules/micro
./nixos/modules/password-store
./nixos/modules/vscode
inputs.nix-index-database.hmModules.nix-index
];
serverHmModules = [
./nixos/modules/home-manager
./nixos/modules/home-manager-server
./nixos/modules/micro
inputs.nix-index-database.hmModules.nix-index
];
nixosModules = [
home-manager.nixosModules.home-manager
inputs.nixos-vscode-server.nixosModules.default
inputs.sops-nix.nixosModules.sops
./nixos/modules/i18n
./nixos/modules/nix
./nixos/modules/qbittorrent
./nixos/modules/rucksack
./nixos/modules/tailscale-autoconnect
(_: {
sops.age.sshKeyPaths = ["/etc/ssh/ssh_host_ed25519_key"];
sops.defaultSopsFile = ./secrets/tailscale.yaml;
sops.secrets.tsauthkey = {};
home-manager = {
useGlobalPkgs = true;
useUserPackages = true;
extraSpecialArgs = {inherit (inputs) dracula-micro;};
in {
overlay = import ./overlays;
defaultApp = deploy-rs.defaultApp;
nixosModules = builtins.listToAttrs (findModules ./modules);
nixosConfigurations = with nixpkgs.lib; let
hosts = builtins.attrNames (builtins.readDir ./machines);
mkHost = name: let
system = builtins.readFile (./machines + "/${name}/system");
pkgs = pkgsFor system;
in
nixosSystem {
inherit system;
modules =
__attrValues self.nixosModules
++ [
inputs.home-manager.nixosModules.home-manager
inputs.sops-nix.nixosModules.sops
{
sops.age.sshKeyPaths = ["/etc/ssh/ssh_host_ed25519_key"];
sops.defaultSopsFile = ./secrets/tailscale.yaml;
sops.secrets.tsauthkey = {};
home-manager = {
useGlobalPkgs = true;
useUserPackages = true;
extraSpecialArgs = {inherit (inputs) dracula-micro;};
users.msfjarvis = {
imports =
(import ./home-manager)
++ [
inputs.nix-index-database.hmModules.nix-index
];
};
};
}
(import (./machines + "/${name}"))
{nixpkgs.pkgs = pkgs;}
];
specialArgs = {inherit inputs;};
};
})
];
mkDesktopConfig = options:
nixpkgs.lib.nixosSystem {
inherit (options) system;
pkgs = pkgs.${options.system};
modules =
nixosModules
++ options.modules
++ [
({lib, ...}: {
home-manager.users.msfjarvis = lib.mkMerge [
{imports = hmModules;}
];
})
];
};
mkNixOSConfig = options:
nixpkgs.lib.nixosSystem {
inherit (options) system;
pkgs = pkgs.${options.system};
modules =
nixosModules
++ options.modules
++ [
({lib, ...}: {
home-manager.users.msfjarvis = lib.mkMerge [
{imports = serverHmModules;}
];
})
];
};
in rec {
darwinConfigurations.work-macbook = darwin.lib.darwinSystem {
in
genAttrs hosts mkHost;
deploy = {
user = "root";
nodes =
builtins.mapAttrs (name: machine: {
hostname = machine.config.networking.hostName;
profiles.system = {
user = "root";
path = deploy-rs.lib.${machine.pkgs.system}.activate.nixos machine;
};
})
self.nixosConfigurations;
};
darwinConfigurations.Harshs-MacBook-Pro = inputs.darwin.lib.darwinSystem rec {
system = "aarch64-darwin";
pkgs = pkgs."aarch64-darwin";
pkgs = pkgsFor system;
modules = [
home-manager.darwinModules.home-manager
./nixos/hosts/work-macbook
inputs.home-manager.darwinModules.home-manager
./darwin
({lib, ...}: {
home-manager.useGlobalPkgs = true;
home-manager.extraSpecialArgs = {inherit (inputs) dracula-micro;};
home-manager.users.msfjarvis = lib.mkMerge [
{imports = hmModules;}
(import ./nixos/hosts/work-macbook/home-manager.nix)
{
imports =
(import ./home-manager)
++ [
inputs.nix-index-database.hmModules.nix-index
];
}
(import ./darwin/home-manager.nix)
];
})
];
};
nixosConfigurations.ryzenbox = mkDesktopConfig {
system = "x86_64-linux";
modules = [
./nixos/hosts/ryzenbox
];
};
nixosConfigurations.crusty = mkNixOSConfig {
system = "aarch64-linux";
modules = [
./nixos/hosts/crusty
inputs.nixos-hardware.nixosModules.raspberry-pi-4
];
};
nixosConfigurations.samosa = mkNixOSConfig {
system = "x86_64-linux";
modules = [
./nixos/hosts/samosa
];
};
nixosConfigurations.wailord = mkNixOSConfig {
system = "x86_64-linux";
modules = [
./nixos/hosts/wailord
];
};
deploy.nodes = {
crusty = {
hostname = "crusty";
fastConnection = true;
remoteBuild = true;
profiles.system = {
sshUser = "root";
path = deploy-rs.lib.aarch64-linux.activate.nixos self.nixosConfigurations.crusty;
user = "root";
};
};
samosa = {
hostname = "samosa";
fastConnection = true;
profiles.system = {
sshUser = "root";
path = deploy-rs.lib.x86_64-linux.activate.nixos self.nixosConfigurations.wailord;
user = "root";
};
};
wailord = {
hostname = "wailord";
fastConnection = true;
profiles.system = {
sshUser = "root";
path = deploy-rs.lib.x86_64-linux.activate.nixos self.nixosConfigurations.wailord;
user = "root";
};
};
};
packages.aarch64-darwin.macbook = darwinConfigurations.work-macbook.system;
apps = forAllSystems (system: {
deploy = deploy-rs.apps.${system}.default;
format = {
type = "app";
program = let
fmtTargetsStr = pkgs.${system}.lib.concatStringsSep " " [
"aliases"
"apps"
"bash_completions.bash"
"common"
"darwin-init"
"devtools"
"files"
"gitshit"
"install.sh"
"minecraft"
"nix"
"pre-push-hook"
"setup/00-android_sdk.sh"
"setup/01-adb_multi.sh"
"setup/02-android_udev.sh"
"shell-init"
"system"
"system_darwin"
"system_linux"
"x"
];
script = pkgs.${system}.writeShellApplication {
name = "format";
runtimeInputs = with pkgs.${system}; [
alejandra
deadnix
shfmt
statix
];
text = ''
shfmt -w -s -i 2 -ci ${fmtTargetsStr};
alejandra --quiet .
deadnix --edit
statix check .
'';
};
in "${script}/bin/format";
};
});
packages.aarch64-darwin.macbook = self.darwinConfigurations.Harshs-MacBook-Pro.system;
};
nixConfig = {
extra-substituters = [

6
home-manager/default.nix Normal file
View File

@ -0,0 +1,6 @@
[
./shared.nix
./micro
./password-store
./vscode
]

View File

@ -9,11 +9,11 @@
fonts.fontconfig.enable = lib.mkDefault true;
targets.genericLinux.enable = true;
targets.genericLinux.enable = lib.mkDefault true;
xdg = lib.mkDefault {
enable = true;
mime.enable = true;
xdg = {
enable = lib.mkDefault true;
mime.enable = lib.mkDefault true;
};
home.file.".imwheelrc".text = ''
@ -58,10 +58,10 @@
++ (import ./packages.nix) pkgs;
programs.atuin = {
enable = true;
enableBashIntegration = true;
flags = ["--disable-up-arrow"];
settings = {
enable = lib.mkDefault true;
enableBashIntegration = lib.mkDefault true;
flags = lib.mkDefault ["--disable-up-arrow"];
settings = lib.mkDefault {
auto_sync = true;
max_preview_height = 2;
search_mode = "skim";
@ -112,9 +112,9 @@
programs.bottom = {enable = lib.mkDefault true;};
programs.browserpass = lib.mkDefault {
enable = true;
browsers = ["firefox"];
programs.browserpass = {
enable = lib.mkDefault true;
browsers = lib.mkDefault ["firefox"];
};
programs.direnv = {
@ -166,9 +166,9 @@
programs.jq = {enable = lib.mkDefault true;};
programs.topgrade = {
enable = true;
enable = lib.mkDefault true;
settings = {
settings = lib.mkDefault {
misc = {
assume_yes = true;
pre_sudo = true;
@ -189,18 +189,20 @@
};
services.gpg-agent = {
enable = true;
defaultCacheTtl = 3600;
pinentryFlavor = "gnome3";
enableBashIntegration = true;
enable = lib.mkDefault true;
defaultCacheTtl = lib.mkDefault 3600;
pinentryFlavor = lib.mkDefault "gnome3";
enableBashIntegration = lib.mkDefault true;
};
services.git-sync = {
enable = true;
repositories.password-store = {
path = config.programs.password-store.settings.PASSWORD_STORE_DIR;
uri = "git+ssh://msfjarvis@github.com:msfjarvis/pass-store.git";
interval = 600;
enable = lib.mkDefault true;
repositories = lib.mkDefault {
password-store = {
path = config.programs.password-store.settings.PASSWORD_STORE_DIR;
uri = "git+ssh://msfjarvis@github.com:msfjarvis/pass-store.git";
interval = 600;
};
};
};
@ -329,25 +331,5 @@
enableBashIntegration = lib.mkDefault true;
};
systemd.user.services.optimise-nix-store = {
Unit = {Description = "nix store maintenance";};
Service = {
CPUSchedulingPolicy = "idle";
IOSchedulingClass = "idle";
ExecStart = toString (pkgs.writeShellScript "nix-optimise-store" ''
${pkgs.nix}/bin/nix-collect-garbage -d
${pkgs.nix}/bin/nix store gc
${pkgs.nix}/bin/nix store optimise
'');
};
};
systemd.user.timers.optimise-nix-store = {
Unit = {Description = "nix store maintenance";};
Timer = {OnCalendar = "weekly";};
Install = {WantedBy = ["timers.target"];};
};
home.stateVersion = "21.05";
}

1
machines/crusty/system Normal file
View File

@ -0,0 +1 @@
aarch64-linux

1
machines/ryzenbox/system Normal file
View File

@ -0,0 +1 @@
x86_64-linux

1
machines/wailord/system Normal file
View File

@ -0,0 +1 @@
x86_64-linux

2
nix
View File

@ -34,7 +34,7 @@ function nixdiff() {
function nixshell() {
[[ -z ${1} ]] && return
BASE_DIR="${SCRIPT_DIR}/nixos/shell-configs"
BASE_DIR="${SCRIPT_DIR}/shell-configs"
declare -a FILES_TO_COPY=()
[[ ! -d "${BASE_DIR}/${1}" ]] && {
reportWarning "No shell config exists for ${1}"

View File

@ -1,56 +0,0 @@
{lib, ...}: {
imports = [
./hardware-configuration.nix
];
boot = {
loader = {
grub.enable = false;
generic-extlinux-compatible.enable = true;
};
tmp.cleanOnBoot = true;
};
zramSwap.enable = true;
time.timeZone = "Asia/Kolkata";
console = {
font = "Lat2-Terminus16";
keyMap = lib.mkForce "us";
useXkbConfig = true;
};
users = {
mutableUsers = false;
users = {
msfjarvis = {
isNormalUser = true;
extraGroups = ["wheel"];
hashedPassword = ''$y$j9T$g8JL/B98ogQF/ryvwHpWe.$jyKMeotGz/o8Pje.nejKzPMiYOxtn//33OzMu5bAHm2'';
openssh.authorizedKeys.keys = [
''ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEoNv1E/D4IzNIJeJg7Rp49Jizw8aoCLSyFLcUmD1F6K''
''ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIP3WC4HKwbfVGnJzhtrWo2Ue0dnaZH1JaPu4X6VILQL6''
];
};
root.openssh.authorizedKeys.keys = [
''ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEoNv1E/D4IzNIJeJg7Rp49Jizw8aoCLSyFLcUmD1F6K''
''ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIP3WC4HKwbfVGnJzhtrWo2Ue0dnaZH1JaPu4X6VILQL6''
];
};
};
networking = {
hostName = "samosa";
networkmanager.enable = true;
nameservers = ["100.100.100.100" "8.8.8.8" "1.1.1.1"];
search = ["tiger-shark.ts.net"];
firewall = {
allowedTCPPorts = [
80
443
];
allowedUDPPorts = [];
};
};
services.openssh.enable = true;
}

View File

@ -1,19 +0,0 @@
{modulesPath, ...}: {
imports = [(modulesPath + "/profiles/qemu-guest.nix")];
boot.loader.efi.efiSysMountPoint = "/boot/efi";
boot.loader.grub = {
efiSupport = true;
efiInstallAsRemovable = true;
device = "nodev";
};
fileSystems."/boot/efi" = {
device = "/dev/disk/by-uuid/1FC5-9E05";
fsType = "vfat";
};
boot.initrd.availableKernelModules = ["ata_piix" "uhci_hcd" "xen_blkfront" "vmw_pvscsi"];
boot.initrd.kernelModules = ["nvme"];
fileSystems."/" = {
device = "/dev/sda1";
fsType = "ext4";
};
}

View File

@ -1,8 +0,0 @@
_: {
programs.starship = {
settings = {
format = "$directory$git_branch$git_state$git_status ";
character.disabled = true;
};
};
}

View File

@ -1,20 +0,0 @@
{pkgs, ...}: binary: drv:
pkgs.symlinkJoin {
name = "${drv.name}-nixglwrapped";
paths = [drv];
nativeBuildInputs = [pkgs.makeWrapper];
postBuild = ''
# This will break if wrapProgram is ever changed, so fingers crossed
makeShellWrapper() {
local original="$1"
local wrapper="$2"
cat << EOF > "$wrapper"
#! ${pkgs.bash}/bin/bash -e
exec "${pkgs.nixgl.auto.nixGLDefault}/bin/nixGL" "$original"
EOF
chmod +x "$wrapper"
}
wrapProgram "$out/bin/${binary}"
'';
}