diff --git a/README.md b/README.md index 7d1ac15..e7fdad4 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,13 @@ flowchart TD * `gacp ` โ€” Shorthand to stage all edits, commit with a message, and push directly to the current branch. * `new-repo ` โ€” Scaffolds local files, runs git init, and pushes the project to GitHub using the `gh` CLI. +### 4. Repository Bundler Utility (`bundle-project`) +* **Script Location:** [modules/user/scripts/bundle_project.py](modules/user/scripts/bundle_project.py) +* **Features:** + * `bundle-project [target_dir] [-o output_file]` โ€” Compresses the structure and contents of a target directory (defaults to `.`) into a single output file (defaults to `output.txt`). + * Natively skips binary files and `.git` repositories to prevent pollution. + * Automatically runs `eza --tree` and outputs it as a visual guide at the header of the bundle file. + --- ## ๐Ÿ”’ Secrets Management (SOPS + age) @@ -153,7 +160,11 @@ Rebuild and switch to the profile matching your active target host machine: ## ๐Ÿ“š Reference Documentation -* [Secrets Management Details](docs/secrets-management.md) โ€” Secure storage configuration using `sops-nix` and `age`. +* [Package & Secrets Workflow Guide](docs/package-and-secrets.md) โ€” Steps for adding custom applications and managing secrets. +* [System Maintenance Guide](docs/system-maintenance.md) โ€” Instructions for safe system updates, health checking, and garbage collection. +* [Declarative Development Environments](docs/development-environments.md) โ€” How to use nix-shell, devShells, direnv, and uv for project isolation. +* [Secrets Management Details](docs/secrets-management.md) โ€” Secure storage bootstrapping using `sops-nix` and `age`. * [Smart DDNS Updater](docs/dynu-ip-monitor.md) โ€” Under-the-hood details of the smart IP change detector and updater. * [Tmux Terminal Multiplexer](docs/tmux.md) โ€” Fast navigation bindings, layouts, and pane splits guide. * [Google Antigravity Setup](docs/antigravity.md) โ€” Technical instructions for packaging and using the Antigravity agent CLI on NixOS. + diff --git a/docs/development-environments.md b/docs/development-environments.md new file mode 100644 index 0000000..b84bae5 --- /dev/null +++ b/docs/development-environments.md @@ -0,0 +1,110 @@ +# ๐Ÿ’ป Declarative Development Environments + +This guide shows how to run isolated project environments using `nix-shell` or `devShells` alongside `direnv` and `uv` to keep the global system environment clean. + +--- + +## โšก DevShells and nix-shell + +Instead of installing packages globally in [apps.nix](file:///home/kiskaadee/Config/modules/user/apps.nix) for one-off projects, define them per-project. + +### 1. Classic `shell.nix` (Nix Channels) +For standard Nix projects, place a `shell.nix` in your project root: +```nix +{ pkgs ? import {} }: + +pkgs.mkShell { + nativeBuildInputs = with pkgs; [ + nodejs_20 + yarn + ]; + + shellHook = '' + echo "Welcome to the Node.js environment!" + ''; +} +``` +Run `nix-shell` to drop into this isolated shell with `node` and `yarn` pre-loaded. + +### 2. Flake-based `devShell` +For modern Flake projects, declare your `devShell` inside your `flake.nix`: +```nix +{ + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + }; + + outputs = { self, nixpkgs }: + let + system = "x86_64-linux"; + pkgs = nixpkgs.legacyPackages.${system}; + in { + devShells.${system}.default = pkgs.mkShell { + buildInputs = with pkgs; [ + go + golangci-lint + ]; + }; + }; +} +``` +Run `nix develop` to enter this environment. + +--- + +## ๐Ÿ”„ Automated Shell Loading with `direnv` + +`direnv` automatically activates your project-specific environment when you `cd` into its directory. + +### Configuration +1. In your project root, create a file named `.envrc`. +2. Add one of the following lines based on your setup: + * **For classic projects (`shell.nix`):** + ```bash + use nix + ``` + * **For Flake-based projects (`flake.nix`):** + ```bash + use flake + ``` +3. Allow the environment to load: + ```bash + direnv allow + ``` + +As soon as you step into the directory, `direnv` populates `$PATH` and other variables with your Nix-defined tools. Leaving the folder unloads them automatically. + +--- + +## ๐Ÿ Python Projects: Nix + `uv` Workflow + +To prevent library conflicts and keep the host system clean, combine Nix (for system-level packages/binaries like C libraries, python compilers) with `uv` (for project-level Python packages). + +### Recommended Workflow: +1. **Use Nix (`devShells` or `nix-shell`)** to make Python and system dependencies (like OpenSSL or PostgreSQL drivers) available: + ```nix + pkgs.mkShell { + buildInputs = with pkgs; [ + python311 + uv + openssl + ]; + } + ``` +2. **Use `uv`** inside that environment to manage the virtual environment and install Python-specific libraries cleanly: + ```bash + # Create the virtualenv + uv venv + + # Activate the environment + source .venv/bin/activate + + # Install dependencies lightning-fast without polluting system packages + uv pip install fastapi uvicorn + ``` +3. **Ensure `.envrc`** loads both the Nix shell and the virtual environment automatically: + ```bash + use flake + layout python + ``` + This triggers standard Nix tools and sources `.venv/bin/activate` simultaneously upon changing directories. diff --git a/docs/package-and-secrets.md b/docs/package-and-secrets.md new file mode 100644 index 0000000..52f3499 --- /dev/null +++ b/docs/package-and-secrets.md @@ -0,0 +1,82 @@ +# ๐Ÿ“ฆ Package & Secrets Workflow Guide + +This guide covers the day-to-day workflow for managing user-space packages and sensitive information (secrets) within this NixOS configuration. + +--- + +## ๐Ÿš€ Adding a New Package + +User-space applications are managed declaratively using Home Manager in [modules/user/apps.nix](file:///home/kiskaadee/Config/modules/user/apps.nix). + +### Step 1: Find the Package Name +Before adding a package, look up its exact attribute name: +* **Search Engine:** Go to the official [NixOS Package Search](https://search.nixos.org/packages). +* **CLI Search:** + ```bash + nix-env -qaP '' + # Or using modern flake-native search: + nix search nixpkgs + ``` + +### Step 2: Update `apps.nix` +Open [modules/user/apps.nix](file:///home/kiskaadee/Config/modules/user/apps.nix) and append the package name directly into the list inside `home.packages`: + +```nix + home.packages = with pkgs; [ + # ... existing packages + + # Text Editors / Development + neovim + + # New Package Example + ripgrep # Modern grep alternative for fast directory search + ]; +``` + +### Step 3: Test and Switch +Verify the package builds correctly before switching system-wide: +```bash +# Test build without applying +nix build .#nixosConfigurations.desktop.config.system.build.toplevel --no-link + +# Apply configuration +sudo nixos-rebuild switch --flake .#desktop +``` + +--- + +## ๐Ÿ”’ Adding a New Secret (SOPS Workflow) + +Sensitive credentials (like passwords, API keys, and environment variables) are managed using `sops-nix`. + +### Step 1: Edit the Secrets File +Launch `sops` inside `nix-shell` to decrypt and edit the target secrets file (e.g., [secrets.yaml](file:///home/kiskaadee/Config/hosts/desktop/secrets.yaml)): +```bash +nix-shell -p sops --run "sops hosts/desktop/secrets.yaml" +``` +Add your key-value pair under the YAML structure: +```yaml +my_new_api_key: "secure_token_goes_here" +``` +*When you save and close your editor, SOPS automatically encrypts the file before saving it back to disk.* + +### Step 2: Declare the Secret in Nix +To make the decrypted secret available to services or applications at runtime, register it in your Nix configuration (e.g., in [hosts/desktop/dynu.nix](file:///home/kiskaadee/Config/hosts/desktop/dynu.nix)): + +```nix +sops.secrets.my_new_api_key = { + # (Optional) Restrict file access to specific users/groups + owner = "root"; + group = "root"; + mode = "0400"; +}; +``` + +### Step 3: Reference the Decrypted Path +Once declared, `sops-nix` exposes the decrypted value under `/run/secrets/` at boot time. You can reference the file path dynamically in your Nix configuration using: + +```nix +# This resolves to "/run/secrets/my_new_api_key" +config.sops.secrets.my_new_api_key.path +``` +Avoid hardcoding raw paths; instead, pass this dynamic path directly to service configurations (e.g., via `EnvironmentFile` or command-line flags). diff --git a/docs/system-maintenance.md b/docs/system-maintenance.md new file mode 100644 index 0000000..b119c5d --- /dev/null +++ b/docs/system-maintenance.md @@ -0,0 +1,78 @@ +# ๐Ÿ› ๏ธ System Maintenance Guide + +This document describes how to update the system safely, verify its health post-update, and clean up disk space by removing old configurations. + +--- + +## ๐Ÿ”„ Performing Clean & Secure System Updates + +NixOS systems managed with Flakes lock dependency versions inside `flake.lock`. To update the system securely, follow this workflow: + +### Step 1: Update the Flake Inputs +Fetch the latest revisions of your inputs (e.g., `nixpkgs`, `home-manager`, and other plugins): +```bash +nix flake update +``` + +### Step 2: Validate the Build & Formatting +Test-build the derivation without applying it to your bootloader. This prevents dirty installations or builder failures (such as Python PEP8 style errors) from affecting system configuration state: +```bash +nix build .#nixosConfigurations.desktop.config.system.build.toplevel --no-link +``` + +### Step 3: Switch & Apply the Update +Once the test build completes successfully, switch to the new generation: +```bash +sudo nixos-rebuild switch --flake .#desktop +``` + +--- + +## ๐Ÿฅ Post-Update Health Checklist + +Verify system health immediately after a major system switch: + +1. **Systemd Service Status:** Ensure all units are running and no critical services failed: + ```bash + systemctl --failed + systemctl --user --failed + ``` +2. **Display & Window Manager:** Verify that your graphics drivers, audio services (PipeWire/WirePlumber), and hotkeys are responsive. +3. **Secrets Integrity:** Check that decrypted secrets are loaded correctly: + ```bash + ls -la /run/secrets/ + ``` +4. **Network Resolution:** Check DNS resolution and active IP rotation daemons: + ```bash + systemctl status dynu-monitor.service + ``` + +--- + +## ๐Ÿงน Cleaning Up Old Generations + +Nix keeps older versions (generations) of your system configuration so you can roll back instantly. However, this takes up storage space. + +### Step 1: List System Generations +To view all available system generations: +```bash +sudo nix-env --list-generations --profile /nix/var/nix/profiles/system +``` + +### Step 2: Delete Old Generations +* **Remove older than X days:** + ```bash + sudo nix-env --delete-generations +5d --profile /nix/var/nix/profiles/system + ``` +* **Remove specific generations:** + ```bash + sudo nix-env --delete-generations 42 43 --profile /nix/var/nix/profiles/system + ``` + +### Step 3: Collect Garbage (Reclaim Disk Space) +Deleting generations only updates configuration links. To actually free up disk space and prune unused dependencies: +```bash +# Run garbage collection for system and user profiles +sudo nix-collect-garbage -d +nix-collect-garbage -d +``` diff --git a/modules/user/apps.nix b/modules/user/apps.nix index 32c09a0..85a2888 100644 --- a/modules/user/apps.nix +++ b/modules/user/apps.nix @@ -1,13 +1,17 @@ -# ๐Ÿ“ฆ User Applications Configuration -# This file declares standalone user-space binaries managed by Home Manager. - { inputs, pkgs, ... }: +let + # Declaratively compile the Python script as a user utility package + bundleProject = pkgs.writers.writePython3Bin "bundle-project" { } (builtins.readFile ./scripts/bundle_project.py); +in { home.packages = with pkgs; [ # Custom Flake injections: Google Antigravity CLI helper inputs.antigravity.packages.${pkgs.stdenv.hostPlatform.system}.google-antigravity-cli + # Local Python utilities + bundleProject + # Media, Note-taking & Graphical Tools mpv # Fast, scriptable, keyboard-driven CLI media player gimp # GNU Image Manipulation Program for graphical assets