mzteinit: initialize PATH before SHELL

This commit is contained in:
LordMZTE 2024-05-12 21:45:58 +02:00
parent f85dfc43dc
commit 4187a0a57f
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
2 changed files with 52 additions and 50 deletions

View file

@ -29,6 +29,50 @@ pub fn populateEnvironment(env: *std.process.EnvMap) !bool {
};
defer alloc.free(home);
// PATH
{
var bufstream = std.io.fixedBufferStream(&buf);
var b = delimitedWriter(bufstream.writer(), ':');
for ([_][]const u8{
".nix-profile/bin",
".mix/escripts",
".cargo/bin",
".local/bin",
"go/bin",
".roswell/bin",
}) |fixed| {
try b.push(try std.fmt.bufPrint(&sbuf, "{s}/{s}", .{ home, fixed }));
}
// racket bins
racket: {
try msg("acquiring racket binary path...", .{});
const res = std.process.Child.run(.{
.allocator = alloc,
.argv = &.{
"racket",
"-l",
"racket/base",
"-e",
"(require setup/dirs) (display (path->string (find-user-console-bin-dir)))",
},
}) catch break :racket;
defer alloc.free(res.stdout);
defer alloc.free(res.stderr);
try b.push(res.stdout);
log.info("racket binary path registered", .{});
}
if (env.get("PATH")) |system_path| {
try b.push(system_path);
}
try env.put("PATH", bufstream.getWritten());
}
try env.put("MZTE_ENV_SET", "1");
// XDG vars
@ -42,9 +86,13 @@ pub fn populateEnvironment(env: *std.process.EnvMap) !bool {
}
// set shell to nu to prevent anything from defaulting to mzteinit
if (try util.findInPath(alloc, "nu")) |fish| {
defer alloc.free(fish);
try env.put("SHELL", fish);
if (try util.findInPath(
alloc,
env.get("PATH") orelse unreachable,
"nu",
)) |nu| {
defer alloc.free(nu);
try env.put("SHELL", nu);
} else {
log.warn("nu not found! setting $SHELL to /bin/sh", .{});
try env.put("SHELL", "/bin/sh");
@ -134,50 +182,6 @@ pub fn populateEnvironment(env: *std.process.EnvMap) !bool {
try std.fmt.bufPrint(&sbuf, "/usr/lib/rofi:{s}/.local/lib/rofi", .{home}),
);
// PATH
{
var bufstream = std.io.fixedBufferStream(&buf);
var b = delimitedWriter(bufstream.writer(), ':');
for ([_][]const u8{
".nix-profile/bin",
".mix/escripts",
".cargo/bin",
".local/bin",
"go/bin",
".roswell/bin",
}) |fixed| {
try b.push(try std.fmt.bufPrint(&sbuf, "{s}/{s}", .{ home, fixed }));
}
// racket bins
racket: {
try msg("acquiring racket binary path...", .{});
const res = std.process.Child.run(.{
.allocator = alloc,
.argv = &.{
"racket",
"-l",
"racket/base",
"-e",
"(require setup/dirs) (display (path->string (find-user-console-bin-dir)))",
},
}) catch break :racket;
defer alloc.free(res.stdout);
defer alloc.free(res.stderr);
try b.push(res.stdout);
log.info("racket binary path registered", .{});
}
if (env.get("PATH")) |system_path| {
try b.push(system_path);
}
try env.put("PATH", bufstream.getWritten());
}
// LUA_CPATH
{
var bufstream = std.io.fixedBufferStream(&buf);

View file

@ -47,9 +47,7 @@ pub fn fmtCommand(cmd: []const []const u8) std.fmt.Formatter(formatCommand) {
return .{ .data = cmd };
}
pub fn findInPath(alloc: std.mem.Allocator, bin: []const u8) !?[]const u8 {
const path = std.posix.getenv("PATH") orelse return null;
pub fn findInPath(alloc: std.mem.Allocator, path: []const u8, bin: []const u8) !?[]const u8 {
var splits = std.mem.split(u8, path, ":");
while (splits.next()) |p| {
const trimmed = std.mem.trim(u8, p, " \n\r");