Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(arx): add experimental macos support via fakedir #107

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions arx-multi-bin.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
diff --git a/model-scripts/tmpx.sh b/model-scripts/tmpx.sh
index 9c2ec6f..21319d6 100755
--- a/model-scripts/tmpx.sh
+++ b/model-scripts/tmpx.sh
@@ -1,9 +1,10 @@
-#!/bin/sh
+#!/usr/bin/env bash
set -e -u
unset rm_ dir
tmp=true ; run=true
tmpdir= ; rm0=true ; rm1=true ; shared=false ; hash="" # To be set by tool.
token=`date -u +%FT%TZ | tr -d :-`-`hexdump -n4 -e '"%08x"' </dev/urandom`
+prog_name="$0"
opts() {
cmd="$1" ; shift
n=$#
@@ -39,9 +40,9 @@ opts() {
if $shared
then
rm_=false
- dir="$tmpdir"/tmpx-"$hash"
+ dir="$HOME"/.cache/tmpx-"$hash"
else
- dir="$tmpdir"/tmpx-"$token"
+ dir="$HOME"/.cache/tmpx-"$token"
fi
: ${rm_:=true}
if $rm_
@@ -53,6 +54,7 @@ opts() {
trap 'exit 2' HUP INT QUIT BUS SEGV PIPE TERM
fi
mkdir -p "$dir"
+ export TMPX_RESTORE_PWD=$(pwd)
cd "$dir"
fi
# Call the command with the reassembled ARGV, options removed.
@@ -72,7 +74,10 @@ go () {
fi
if $run
then
- ( . ../env && exec ../run "$@" )
+ (
+ . ../env
+ exec -a "${prog_name}" bash -c ". ../run" "${prog_name}" "$@"
+ )
fi
}
unpack_env () { : # NOOP
38 changes: 27 additions & 11 deletions default.nix
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{nixpkgs ? import <nixpkgs> {}}:
{
nixpkgs ? import <nixpkgs> {}
, fakedir ? import (fetchTarball "https://github.com/nixie-dev/fakedir/archive/b0f70b805aa86fe881eeca642f12069c7983a8e7.tar.gz") {}
}:

with nixpkgs;

let
arx' = haskellPackages.arx.overrideAttrs (o: {
patchPhase = (o.patchPhase or "") + ''
substituteInPlace model-scripts/tmpx.sh \
--replace /tmp/ \$HOME/.cache/
'';
patches = (o.patches or []) ++ [./arx-multi-bin.patch];
});
in rec {
toStorePath = target:
Expand All @@ -24,23 +24,25 @@ in rec {
stdenv.mkDerivation {
name = "arx";
buildCommand = ''
${arx'}/bin/arx tmpx --shared -rm! ${archive} -o $out // ${startup}
${arx'}/bin/arx tmpx --shared -rm! ${archive} -o $out -e ${startup}
chmod +x $out
'';
};

maketar = { targets }:
stdenv.mkDerivation {
name = "maketar";
buildInputs = [ perl ];
buildInputs = [ perl gnutar ];
exportReferencesGraph = map (x: [("closure-" + baseNameOf x) x]) targets;
buildCommand = ''
buildCommand = let
additionalPaths = lib.optionalString stdenv.isDarwin "-C ${fakedir} lib";
in ''
storePaths=$(perl ${pathsFromGraph} ./closure-*)

tar -cf - \
--owner=0 --group=0 --mode=u+rw,uga+r \
--hard-dereference \
$storePaths | bzip2 -z > $out
$storePaths ${additionalPaths} | bzip2 -z > $out
'';
};

Expand Down Expand Up @@ -88,18 +90,32 @@ in rec {
let
# Avoid re-adding a store path into the store
path = toStorePath target;
script-linux = ''
exec .${nix-user-chroot'}/bin/nix-user-chroot -n ./nix -w ''${TMPX_RESTORE_PWD} ${nixUserChrootFlags} -- ".$(basename "$0")/$(dirname ${path}${run})" "$@"
'';
script-macos = ''
# use absolute paths so the environment variables don't get reinterpreted after a cd
__TMPX_DAT_PATH=$(pwd)
cd "''${TMPX_RESTORE_PWD}"
export DYLD_INSERT_LIBRARIES="''${__TMPX_DAT_PATH}/lib/libfakedir.dylib"
export FAKEDIR_PATTERN=/nix
export FAKEDIR_TARGET="''${__TMPX_DAT_PATH}/nix"

exec "''${__TMPX_DAT_PATH}$(basename "$0")/$(dirname ${path}${run})/$(basename "$0")" "$@"
'';
script = if stdenv.isDarwin then script-macos else script-linux;
in
writeScript "startup" ''
#!/bin/sh
${initScript}
.${nix-user-chroot'}/bin/nix-user-chroot -n ./nix ${nixUserChrootFlags} -- ${path}${run} "$@"
${script}
'';

nix-bootstrap = { target, extraTargets ? [], run, nix-user-chroot' ? nix-user-chroot, nixUserChrootFlags ? "", initScript ? "" }:
let
script = makeStartup { inherit target nixUserChrootFlags nix-user-chroot' run initScript; };
in makebootstrap {
startup = ".${script} '\"$@\"'";
startup = script;
targets = [ "${script}" ] ++ extraTargets;
};

Expand Down
76 changes: 76 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 27 additions & 10 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
{
description = "The purely functional package manager";

inputs.nixpkgs.url = "nixpkgs/nixos-20.03-small";
inputs.nixpkgs.url = "nixpkgs/nixos-24.05-small";
inputs.fakedir-pkgs.url = "github:nixie-dev/fakedir";

outputs = { self, nixpkgs }: let
systems = [ "x86_64-linux" "i686-linux" "aarch64-linux" ];
forAllSystems = f: nixpkgs.lib.genAttrs systems (system: f system);
in {
outputs = { self, nixpkgs, fakedir-pkgs }: {
bundlers = {
nix-bundle = { program, system }: let
nixpkgs' = nixpkgs.legacyPackages.${system};
nix-bundle = import self { nixpkgs = nixpkgs'; };
script = nixpkgs'.writeScript "startup" ''
#!/bin/sh
.${nix-bundle.nix-user-chroot}/bin/nix-user-chroot -n ./nix -- ${program} "$@"
fakedir = fakedir-pkgs.packages.${system}.fakedir-universal;
nix-bundle = import self { nixpkgs = nixpkgs'; inherit fakedir; };
script-linux = nixpkgs'.writeScript "startup" ''
#!/usr/bin/env bash
exec .${nix-bundle.nix-user-chroot}/bin/nix-user-chroot -n ./nix -w "''${TMPX_RESTORE_PWD}" -- "$(dirname ${program})/$(basename $0)" "$@"
'';
script-darwin = nixpkgs'.writeScript "startup" ''
#!/usr/bin/env bash
# use absolute paths so the environment variables don't get reinterpreted after a cd
__TMPX_DAT_PATH=$(pwd)
cd "''${TMPX_RESTORE_PWD}"
export DYLD_INSERT_LIBRARIES="''${__TMPX_DAT_PATH}/lib/libfakedir.dylib"
export FAKEDIR_PATTERN=/nix
export FAKEDIR_TARGET="''${__TMPX_DAT_PATH}/nix"

# make sure the fakedir libraries are loaded by running the command in bash within the bottle
cmd="$(cat <<-EOH
''${__TMPX_DAT_PATH}$(dirname ${program})/$(basename $0)
EOH
)"
args="$@"
exec "''${__TMPX_DAT_PATH}/${nixpkgs'.bash}/bin/bash" -c "$cmd $args"
'';
script = if nixpkgs'.stdenv.isDarwin then script-darwin else script-linux;
in nix-bundle.makebootstrap {
targets = [ script ];
startup = ".${builtins.unsafeDiscardStringContext script} '\"$@\"'";
startup = script;
};
};

Expand Down
2 changes: 1 addition & 1 deletion nix-user-chroot/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ENV_PATH ?= ""

nix-user-chroot: main.cpp
${CXX} -o nix-user-chroot -DENV_PATH='$(ENV_PATH)' main.cpp
${CXX} -std=c++20 -o nix-user-chroot -DENV_PATH='$(ENV_PATH)' main.cpp
Loading