From 1113554bf0b6bfc58e92e246444627aea7c0f5f3 Mon Sep 17 00:00:00 2001 From: LordMZTE Date: Mon, 11 Mar 2024 14:47:13 +0100 Subject: [PATCH] fix mzteinit on nix --- flake.lock | 110 +--------------------------------- flake.nix | 10 +--- scripts/mzteinit/package.nix | 63 +++++++------------ scripts/mzteinit/src/env.zig | 8 ++- scripts/mzteinit/src/util.zig | 27 +++++++++ 5 files changed, 60 insertions(+), 158 deletions(-) diff --git a/flake.lock b/flake.lock index 5dc1940..a6047ee 100644 --- a/flake.lock +++ b/flake.lock @@ -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", diff --git a/flake.nix b/flake.nix index 3804c8f..60df1c5 100644 --- a/flake.nix +++ b/flake.nix @@ -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"; diff --git a/scripts/mzteinit/package.nix b/scripts/mzteinit/package.nix index dde9274..a236bc0 100644 --- a/scripts/mzteinit/package.nix +++ b/scripts/mzteinit/package.nix @@ -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"; } diff --git a/scripts/mzteinit/src/env.zig b/scripts/mzteinit/src/env.zig index 4c38ea2..f41dd01 100644 --- a/scripts/mzteinit/src/env.zig +++ b/scripts/mzteinit/src/env.zig @@ -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"); diff --git a/scripts/mzteinit/src/util.zig b/scripts/mzteinit/src/util.zig index 1c76559..5193b90 100644 --- a/scripts/mzteinit/src/util.zig +++ b/scripts/mzteinit/src/util.zig @@ -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; +}