Skip to content

Commit cb7c69f

Browse files
committed
Integrate init-app.nu for system path initialization to standard
- observe '/etc/environment' and compute standard path - update comments and tested - depends on ~/.local/sdkman/bin/sdkman-init.sh and ~/.local/sdkman/src/sdkman-path-helpers.sh path
1 parent 3c3b3fe commit cb7c69f

File tree

2 files changed

+72
-25
lines changed

2 files changed

+72
-25
lines changed

init-app.nu

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
#!/usr/bin/env nu
22
#------------------------------------------------------------------------------
33
# .SYNOPSIS
4-
# Initialize Specified Application
4+
# Initialize application-specific env path
5+
#
56
# .DESCRIPTION
6-
# TODO: Add Linux support (remove ~/bin when it doesn't exist)
7-
# Initializes shell/env for application
8-
# rewrite of pwsh/Init-App.ps1 *
7+
# Resets PATH to platform-specific defaults or adds application paths.
8+
# Notes:
9+
# - currently 2 apps: system default and git
10+
# - system defaults with SDKMAN tools on Unix/Linux
11+
# - On Windows, PFiles is currently used by git
12+
#
13+
# .PARAMETER app_name
14+
# Application to initialize: 'git', 'reset-env-path' (default)
915
#
1016
# .EXAMPLE
11-
# init-app reset-env-path
17+
# source ./init-app.nu
18+
# $env.Path = (main APP_NAME)
1219
#
1320
# .NOTES
14-
# Targeting apps i.e., choco, python (ML).
15-
# Required Env Vars:
16-
# - $pfiles_x64_dir
21+
# Platform-specific behavior:
22+
# - Windows: Uses SystemRoot, PFiles_x64 paths (overlaps with choco)
23+
# - Unix/Linux: Includes SDKMAN (Java, Kotlin, Gradle) + system paths
24+
# - Unix/Linux: Warns if current PATH differs from expected baseline
25+
# - Target apps: git, choco, python (ML) etc. more in future
1726
#------------------------------------------------------------------------------
1827

1928
def main [
@@ -26,22 +35,55 @@ def main [
2635

2736
match $app_name {
2837
'git' => {
29-
let git_path = ($pfiles_x64_dir | path join 'git' 'cmd')
30-
let path_with_git = ($prior_env_path | append $git_path)
31-
$path_with_git
38+
# not required in Unix, return default
39+
if ($nu.os-info.name != "windows") {
40+
$prior_env_path
41+
} else {
42+
let git_path = ($pfiles_x64_dir | path join 'git' 'cmd')
43+
let path_with_git = ($prior_env_path | append $git_path)
44+
$path_with_git
45+
}
3246
}
3347
'reset-env-path' => {
34-
# Reset PATH to default
35-
[
36-
($env.SystemRoot | path join 'system32'),
37-
$env.SystemRoot,
38-
($env.SystemRoot | path join 'System32' 'Wbem'),
39-
($env.LOCALAPPDATA | path join 'Microsoft' 'WindowsApps'),
40-
($env.SystemRoot | path join 'System32' 'OpenSSH'),
41-
# ($env.SystemRoot | path join 'System32' 'WindowsPowerShell' 'v1.0'),
42-
"C:\\PFiles_x64\\bin", # /usr/bin emulation
43-
$ShellHome
44-
]
48+
# Reset PATH to platform-specific default
49+
if ($nu.os-info.name != "windows") {
50+
# backup of /etc/environment, pop_os 10-03-2025
51+
# $ cat /etc/environment
52+
# PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
53+
54+
# Expected default system PATH for comparison
55+
let expected_system_path = [
56+
"/usr/bin",
57+
"/usr/sbin",
58+
"/usr/local/bin",
59+
"/home/atiq/.local/sdkman/candidates/kotlin/current/bin",
60+
"/home/atiq/.local/sdkman/candidates/java/current/bin",
61+
"/home/atiq/.local/sdkman/candidates/gradle/current/bin"
62+
]
63+
64+
# Check if current PATH differs from expected
65+
if ($env.Path != $expected_system_path) {
66+
print "WARN: System environment PATH has changed from expected defaults:"
67+
print $"Actual: ($env.Path)"
68+
print ""
69+
print $"Expected: ($expected_system_path)"
70+
print ""
71+
}
72+
73+
# return standard system path with our shell's dir appended
74+
($expected_system_path | append $ShellHome)
75+
} else {
76+
[
77+
($env.SystemRoot | path join 'system32'),
78+
$env.SystemRoot,
79+
($env.SystemRoot | path join 'System32' 'Wbem'),
80+
($env.LOCALAPPDATA | path join 'Microsoft' 'WindowsApps'),
81+
($env.SystemRoot | path join 'System32' 'OpenSSH'),
82+
# ($env.SystemRoot | path join 'System32' 'WindowsPowerShell' 'v1.0'),
83+
"C:\\PFiles_x64\\bin", # /usr/bin emulation
84+
$ShellHome
85+
]
86+
}
4587
}
4688
_ => {
4789
print $"Invalid command line argument: ($app_name)"

init.nu

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ print $"NuShell ($env.NU_VERSION) on ($nu.os-info.name) ($nu.os-info.kernel_vers
6363

6464
print ""
6565

66+
source ./init-app.nu
67+
# print $"Path: (main reset-env-path)"
68+
$env.Path = (main reset-env-path)
69+
$env.Path = (main git)
70+
71+
6672
# Porting tasks TODO
6773
# - InitConsoleUI, and probably
6874
# - app specific adjustments that are coming from sdkman
@@ -71,9 +77,6 @@ print ""
7177
# Hence, utilize the method/custom command, yet, assign it here
7278
# resetEnvPath and `git add` support on Win below
7379
if ($nu.os-info.name == "windows") {
74-
source ./init-app.nu
75-
$env.Path = (main reset-env-path)
76-
$env.Path = (main git)
7780
source ./win/init.nu
7881
} else {
7982
# ssh initialization
@@ -87,6 +90,8 @@ if ($nu.os-info.name == "windows") {
8790
# OS dependent inits for same instructions, differnet invocation syntax
8891
# since windows nushell keeps throwing error
8992
if ($nu.os-info.name == "windows") {
93+
# TODO: try following on windows, if it works might as well generalize
94+
# nu ./fs-helper.nu
9095
# this is separately invoked on windows since it throws an error
9196
.\fs-helper.nu
9297
} else {

0 commit comments

Comments
 (0)