File tree Expand file tree Collapse file tree 1 file changed +64
-0
lines changed Expand file tree Collapse file tree 1 file changed +64
-0
lines changed Original file line number Diff line number Diff line change 1+ {
2+ inputs ,
3+ lib ,
4+ config ,
5+ pkgs ,
6+ ...
7+ } :
8+
9+ {
10+ perSystem =
11+ { system , pkgs , ... } :
12+ {
13+ packages . set-git-env = pkgs . writeShellApplication {
14+ name = "set-git-env" ;
15+ runtimeInputs = with pkgs ; [
16+ git
17+ coreutils
18+ gnused
19+ gnugrep
20+ ] ;
21+ text = ''
22+ #!/usr/bin/env bash
23+
24+ set -euo pipefail
25+
26+ update_or_append() {
27+ local var_name=$1
28+ local var_value=$2
29+ local file=".env"
30+
31+ # Create .env file if it doesn't exist
32+ if [ ! -f "$file" ]; then
33+ touch "$file"
34+ fi
35+
36+ if [ -s "$file" ] && [ "$(tail -c1 "$file"; echo x)" != $'\nx' ]; then
37+ echo >> "$file"
38+ fi
39+
40+ if grep -q "^$var_name=" "$file"; then
41+ # Use sed with backup extension for compatibility
42+ sed -i.bak "s/^$var_name=.*/$var_name=$var_value/" "$file"
43+ # Remove backup file
44+ rm -f "$file.bak"
45+ else
46+ echo "$var_name=$var_value" >> "$file"
47+ fi
48+ }
49+
50+ GIT_REPO_NAME=$(basename -s .git "$(git config --get remote.origin.url 2>/dev/null || echo "unknown-repo")")
51+ update_or_append "GIT_REPO_NAME" "$GIT_REPO_NAME"
52+
53+ GIT_REF=$(git symbolic-ref -q --short HEAD 2>/dev/null || git rev-parse HEAD 2>/dev/null || echo "unknown-ref")
54+ update_or_append "GIT_REF" "$GIT_REF"
55+
56+ GIT_SHA=$(git rev-parse HEAD 2>/dev/null || echo "unknown-sha")
57+ update_or_append "GIT_SHA" "$GIT_SHA"
58+
59+ GIT_SHA_SHORT=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")
60+ update_or_append "GIT_SHA_SHORT" "$GIT_SHA_SHORT"
61+ '' ;
62+ } ;
63+ } ;
64+ }
You can’t perform that action at this time.
0 commit comments