fix mzteinit on nix

This commit is contained in:
LordMZTE 2024-03-11 14:47:13 +01:00
parent 4d713eef83
commit 1113554bf0
Signed by: LordMZTE
GPG Key ID: B64802DC33A64FF6
5 changed files with 60 additions and 158 deletions

View File

@ -1,81 +1,12 @@
{
"nodes": {
"confgen": {
"inputs": {
"nixpkgs": "nixpkgs",
"nixpkgs-zig-0-12": "nixpkgs-zig-0-12",
"utils": "utils"
},
"locked": {
"lastModified": 1710099294,
"narHash": "sha256-u4TQiW0IODAjGRGy1igEQsaZ6P/xI+RVyzZqrCR4vfI=",
"ref": "a6fbe3c79eeed1dbda04a0be501fa2b95450a03f",
"rev": "a6fbe3c79eeed1dbda04a0be501fa2b95450a03f",
"revCount": 43,
"type": "git",
"url": "https://git.mzte.de/LordMZTE/confgen"
},
"original": {
"rev": "a6fbe3c79eeed1dbda04a0be501fa2b95450a03f",
"type": "git",
"url": "https://git.mzte.de/LordMZTE/confgen"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1709961763,
"narHash": "sha256-6H95HGJHhEZtyYA3rIQpvamMKAGoa8Yh2rFV29QnuGw=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "3030f185ba6a4bf4f18b87f345f104e6a6961f34",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs-zig-0-12": {
"locked": {
"lastModified": 1710092316,
"narHash": "sha256-96pThxPueCevkteIFsx2lMDcCjDrQtBfEbIJR2g+C5Q=",
"owner": "vancluever",
"repo": "nixpkgs",
"rev": "add3752a30c1d9caa7614208afcb8e5502ce4407",
"type": "github"
},
"original": {
"owner": "vancluever",
"ref": "vancluever-zig-0-12",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs-zig-0-12_2": {
"locked": {
"lastModified": 1710092316,
"narHash": "sha256-96pThxPueCevkteIFsx2lMDcCjDrQtBfEbIJR2g+C5Q=",
"owner": "vancluever",
"repo": "nixpkgs",
"rev": "add3752a30c1d9caa7614208afcb8e5502ce4407",
"type": "github"
},
"original": {
"owner": "vancluever",
"ref": "vancluever-zig-0-12",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1709703039,
"narHash": "sha256-6hqgQ8OK6gsMu1VtcGKBxKQInRLHtzulDo9Z5jxHEFY=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "9df3e30ce24fd28c7b3e2de0d986769db5d6225d",
"rev": "3030f185ba6a4bf4f18b87f345f104e6a6961f34",
"type": "github"
},
"original": {
@ -87,10 +18,8 @@
},
"root": {
"inputs": {
"confgen": "confgen",
"nixpkgs": "nixpkgs_2",
"nixpkgs-zig-0-12": "nixpkgs-zig-0-12_2",
"utils": "utils_2"
"nixpkgs": "nixpkgs",
"utils": "utils"
}
},
"systems": {
@ -108,21 +37,6 @@
"type": "github"
}
},
"systems_2": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
},
"utils": {
"inputs": {
"systems": "systems"
@ -140,24 +54,6 @@
"repo": "flake-utils",
"type": "github"
}
},
"utils_2": {
"inputs": {
"systems": "systems_2"
},
"locked": {
"lastModified": 1709126324,
"narHash": "sha256-q6EQdSeUZOG26WelxqkmR7kArjgWCdw5sfJVHPH/7j8=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "d465f4819400de7c8d874d50b982301f28a84605",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
}
},
"root": "root",

View File

@ -2,27 +2,19 @@
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
utils.url = "github:numtide/flake-utils";
confgen.url = "git+https://git.mzte.de/LordMZTE/confgen?rev=a6fbe3c79eeed1dbda04a0be501fa2b95450a03f";
nixpkgs-zig-0-12.url = "github:vancluever/nixpkgs/vancluever-zig-0-12";
};
outputs =
{ self
, nixpkgs
, utils
, confgen
, nixpkgs-zig-0-12
}: utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; };
flakePkg = ref: (builtins.getFlake ref).packages.${system}.default;
in
{
packages.mzteinit = import ./scripts/mzteinit/package.nix {
inherit pkgs;
confgen = confgen.packages.${system};
zig_0_12 = nixpkgs-zig-0-12.legacyPackages.${system}.zig_0_12;
};
mzteinit = pkgs.callPackage ./scripts/mzteinit/package.nix { };
# Local user nix env
packages.mzte-nix = pkgs.symlinkJoin {
name = "mzte-nix";

View File

@ -1,47 +1,28 @@
{ pkgs, confgen, zig_0_12 }:
let
deps = pkgs.linkFarm "zig-packages" [
# ansi-term
{
name = "1220ea86ace34b38e49c1d737c5f857d88346af10695a992b38e10cb0a73b6a19ef7";
path = pkgs.fetchgit {
url = "https://github.com/LordMZTE/ansi-term.git";
rev = "73c03175068679685535111dbea72cade075719e";
hash = "sha256-YeCZPUNciJz141HSHk4kBIfVYW/JqLflkKCjRHhIORk=";
};
}
];
in
pkgs.stdenv.mkDerivation {
{ pkgs, lib }:
{ setEnvironment ? null }:
pkgs.writeShellApplication {
name = "mzteinit";
# TODO: WTF
src = ./../..;
dontBuild = true;
dontFixup = true;
configurePhase = ''
mkdir cgout
# TODO: WTF
sed -i 's#/usr/share/lua/5.4/fennel.lua#${pkgs.luajitPackages.fennel}/share/lua/5.1/fennel.lua#' confgen.lua
${confgen.default}/bin/confgen --json-opt confgen.lua > cgout/opts.json
# We need a wrapper script here because nix cannot build mzteinit while taking localconf into
# account, as the builder has no access to the home directory. Thus, the user must build
# mzteinit and we need to launch it here (before it's contained in $PATH, hence the absolute path).
text = ''
${lib.optionalString (setEnvironment != null) ''
if [ -z "$__NIXOS_SET_ENVIRONMENT_DONE" ]; then
# shellcheck disable=SC1091
. ${setEnvironment}
fi
'' }
mzteinit_path="$HOME"/.local/bin/mzteinit
if [[ -f "$mzteinit_path" ]]; then
exec $mzteinit_path
else
echo "mzteinit not found, starting pre-launch emergency shell!"
exec ${pkgs.bash}/bin/bash
fi
'';
postPatch = ''
cd scripts/mzteinit
export ZIG_LOCAL_CACHE_DIR=$(pwd)/zig-cache
export ZIG_GLOBAL_CACHE_DIR=$ZIG_LOCAL_CACHE_DIR
mkdir -p $ZIG_GLOBAL_CACHE_DIR
ln -s ${deps} $ZIG_GLOBAL_CACHE_DIR/p
cd ../..
'';
bashOptions = [ "errexit" "pipefail" ];
installPhase = ''
cd scripts/mzteinit
runHook preBuild
${zig_0_12}/bin/zig build install --prefix $out
runHook postBuild
cd ../..
'';
passthru.shellPath = "/bin/mzteinit";
derivationArgs.passthru.shellPath = "/bin/mzteinit";
}

View File

@ -42,7 +42,13 @@ pub fn populateEnvironment(env: *std.process.EnvMap) !bool {
}
// set shell to fish to prevent anything from defaulting to mzteinit
try env.put("SHELL", "/usr/bin/fish");
if (try util.findInPath(alloc, "fish")) |fish| {
defer alloc.free(fish);
try env.put("SHELL", fish);
} else {
log.warn("fish not found! setting $SHELL to /bin/sh", .{});
try env.put("SHELL", "/bin/sh");
}
// mix (elixir package manager) should respect XDG
try env.put("MIX_XDG", "1");

View File

@ -46,3 +46,30 @@ fn formatCommand(
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.os.getenv("PATH") orelse return null;
var splits = std.mem.split(u8, path, ":");
while (splits.next()) |p| {
const trimmed = std.mem.trim(u8, p, " \n\r");
if (trimmed.len == 0)
continue;
const joined = try std.fs.path.joinZ(
alloc,
&.{ trimmed, bin },
);
_ = std.fs.cwd().statFile(joined) catch |e| {
alloc.free(joined);
switch (e) {
error.FileNotFound, error.AccessDenied => continue,
else => return e,
}
};
return joined;
}
return null;
}