From cd045e99ad78ffbd55f1a601b2c39440ee9286b8 Mon Sep 17 00:00:00 2001 From: "RingOfStorms (Joshua Bell)" Date: Thu, 2 Jan 2025 01:24:01 -0600 Subject: [PATCH] add worktree ease for local dev of my modules system --- .envrc | 4 ++++ .gitignore | 2 +- flake.nix | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ readme.md | 33 ++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 .envrc diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..aa374c2 --- /dev/null +++ b/.envrc @@ -0,0 +1,4 @@ +#!/bin/bash + +use flake + diff --git a/.gitignore b/.gitignore index c3f3b97..c19f0ef 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ # if in /etc/nixos don't grab defaults ./hardware-configuration.nix ./configuration.nix - +modules/ diff --git a/flake.nix b/flake.nix index 874cbf0..30b33b1 100644 --- a/flake.nix +++ b/flake.nix @@ -63,6 +63,7 @@ { self, nypkgs, + nixpkgs_stable, cosmic, lio_nixpkgs, lio_home-manager, @@ -147,6 +148,15 @@ hostsDir = ./hosts_old; usersDir = ./users; }; + + # Utilities + inherit (nixpkgs_stable) lib; + # Define the systems to support (all Linux systems exposed by nixpkgs) + systems = lib.intersectLists lib.systems.flakeExposed lib.platforms.linux; + forAllSystems = lib.genAttrs systems; + # Create a mapping from system to corresponding nixpkgs : https://nixos.wiki/wiki/Overlays#In_a_Nix_flake + nixpkgsFor = forAllSystems (system: nixpkgs_stable.legacyPackages.${system}); + in { # foldl' is "reduce" where { } is the accumulator and myHosts is the array to reduce on. @@ -186,5 +196,51 @@ // nixConfig.opts; } ) { } myHosts; + + devShells = forAllSystems ( + system: + let + pkgs = nixpkgsFor.${system}; + + mod_worktrees = pkgs.writeShellScriptBin "mod_worktrees" '' + # Get all local and remote mod_* branches, removing lines with '+' + branches=$(git branch -a | grep -E 'mod_' | grep -v '^\s*+' | sed 's/^[* ]*//; s/^remotes\/origin\///') + + # Remove duplicates and sort + branches=$(echo "$branches" | sort -u) + + for branch in $branches; do + # Skip master or other non-mod branches + if [[ ! "$branch" =~ ^mod_ ]]; then + continue + fi + + # Derive module name (remove mod_ prefix) + module_name="''${branch#mod_}" + module_path="modules/$module_name" + + # Check if worktree already exists + if [ ! -d "$module_path" ]; then + echo "Adding worktree for $branch in $module_path" + git worktree add "$module_path" "$branch" 2>/dev/null + # else + # echo "Worktree for $branch already exists" + fi + done + ''; + in + { + default = pkgs.mkShell { + nativeBuildInputs = [ + mod_worktrees + ]; + + shellHook = '' + mod_worktrees + ''; + }; + } + ); + }; } diff --git a/readme.md b/readme.md index 0a625d7..b869e0d 100644 --- a/readme.md +++ b/readme.md @@ -4,6 +4,39 @@ +## Setup local dev + +* Get modules in the same directory for ease of development +```sh +#!/bin/bash + +# Get all local and remote mod_* branches, removing lines with '+' +branches=$(git branch -a | grep -E 'mod_' | grep -v '^\s*+' | sed 's/^[* ]*//; s/^remotes\/origin\///') + +# Remove duplicates and sort +branches=$(echo "$branches" | sort -u) + +for branch in $branches; do + # Skip master or other non-mod branches + if [[ ! "$branch" =~ ^mod_ ]]; then + continue + fi + + # Derive module name (remove mod_ prefix) + module_name="${branch#mod_}" + module_path="modules/$module_name" + + # Check if worktree already exists + if [ ! -d "$module_path" ]; then + echo "Adding worktree for $branch in $module_path" + git worktree add "$module_path" "$branch" 2>/dev/null + else + echo "Worktree for $branch already exists" + fi +done + +``` + # First Install on new Machine ## NixOS install