Skip to content

run arbitrary binaries from the nix store #164

@milahu

Description

@milahu

priority: has workaround

difficulty: trivial

actual

$1 is appended to
/home/user/.nix-portable/nix/store/j823gnvpxh1fjibycmldrspkk3pgivwp-nix-2.20.6/bin/

$ nix-portable /nix/store/xxx/bin/foo
proot error: '/home/user/.nix-portable/nix/store/j823gnvpxh1fjibycmldrspkk3pgivwp-nix-2.20.6/bin//home/user/.nix-portable/nix/store/hbpshlwkgs3qs8r35k8mindh8ansavph-bash-interactive-5.2p26/bin/bash' not found (root = /home/user/.nix-portable/emptyroot, cwd = /home/user/test, $PATH=(null))

$ nix-portable ../../../../nix/store/xxx/bin/foo
foo!

expected

$1 should be joined with the path
/home/user/.nix-portable/nix/store/j823gnvpxh1fjibycmldrspkk3pgivwp-nix-2.20.6/bin/
so when $1 is an absolute path, the other path should be ignored

$ nix-portable /nix/store/xxx/bin/foo
foo!
function join_paths

https://stackoverflow.com/questions/79778815/join-multiple-paths-in-bash

#!/usr/bin/env bash

function join_paths() {
    # Bash version of Python's os.path.join function
    # https://docs.python.org/3.9/library/os.path.html#os.path.join
    # https://stackoverflow.com/a/79778845/10440128
    [ $# = 0 ] && return 1
    local a=("$@") s=${a[-1]} i
    for (( i=$#-2; i>=0; --i )); do
        [[ $s = /* ]] && break
        s=${a[i]%/}${a[i]:+/}$s
    done
    printf '%s' "$s" # echo would break on s="-n"
}

# test
function test_join_paths() {
    expected_rc="$1"
    expected="$2"
    shift 2
    # https://stackoverflow.com/a/79778844/10440128
    # expected="$(python -c 'import os, sys; print(os.path.join(*sys.argv[1:]))' "$@" 2>/dev/null)"; expected_rc=$?
    actual="$(join_paths "$@" 2>&1)"
    actual_rc=$?
    if [ "$expected" != "$actual" ] || [ "$expected_rc" != "$actual_rc" ]; then
        echo -n "FIXME test_join_paths"
        for a in "$expected" "$@"; do printf " %q" "$a"; done
        echo " # actual: ${actual@Q} # expected_rc: $expected_rc # actual_rc: $actual_rc"
    fi
}
test_join_paths 1 ""
test_join_paths 0 "" ''
test_join_paths 0 "" '' ''
test_join_paths 0 "a/b/c" a b c
test_join_paths 0 "a/b/c/" a/ b/ c/
test_join_paths 0 "a//b//c//" a// b// c//
test_join_paths 0 "a///b///c///" a/// b/// c///
test_join_paths 0 "a/b/../c" a b ../c
test_join_paths 0 "/c" a b /c
test_join_paths 0 "//c" a b //c
test_join_paths 0 "////c" a b ////c
test_join_paths 0 "a/b/c" a b '' c
test_join_paths 0 "////c/" a b ////c ''
test_join_paths 0 "a a a/b b/c" 'a a a' 'b b' c
test_join_paths 0 $'a\na/b\tb/c\rc' $'a\na' $'b\tb' $'c\rc'
test_join_paths 0 "a/b//c/d" a b//c d
test_join_paths 0 "-n" '-n'

use case

i need this for my nix-build-debug
where i build a custom bash shell with nix-build
and then i want to run that bash shell
so currently im using the workaround
nix-portable ../../../../nix/store/xxx/bin/bash

this workaround would continue to work with join_paths

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions