Skip to content

Commit

Permalink
feat: codchiw.exe for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
htngr committed Nov 22, 2024
1 parent 9f6be1e commit dd2232e
Show file tree
Hide file tree
Showing 14 changed files with 513 additions and 258 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jobs:
- run: |
nix build .#windows -L
cp result/bin/*.exe .
nix build .#codchiw-windows -L
cp result/bin/*.exe .
nix build .#store-wsl.config.build.tarball -L
cp result store.tar.gz
nix build .#machine-wsl.config.build.tarball -L
Expand Down
File renamed without changes.
224 changes: 224 additions & 0 deletions build/build-rust-package.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
{ self
, runCommand
, runCommandLocal
, runtimeShell
, writeShellScript
, writeShellScriptBin
, fetchFromGitHub

, lib

, store-lxd-tarball
, machine-lxd-tarball
, platform

, makeRustPlatform
, rust-bin

, makeWrapper
, pkg-config
, gtk3
, libayatana-appindicator

, llvmPackages
, cargo-xwin
, wine64

, installShellFiles
, pandoc
, ...
}:

args:

let
rustConfig = {
extensions = [
"rust-src"
"rust-analyzer"
];
targets = [
"x86_64-unknown-linux-gnu"
"x86_64-pc-windows-msvc"
"wasm32-wasi"
];
};
# rustOrig = rust-bin.stable.latest.default.override rustConfig;
rustOrig = rust-bin.selectLatestNightlyWith (toolchain: toolchain.default.override rustConfig);
rustPlatformOrig = makeRustPlatform { cargo = rustOrig; rustc = rustOrig; };
xwin = rustPlatformOrig.buildRustPackage rec {
name = "xwin";
src = fetchFromGitHub {
owner = "Jake-Shadle";
repo = "xwin";
rev = "0.5.0";
sha256 = "sha256-qHlh1PjEzm8nJW3IemikCaaxLtUCZRQccGQg/DgnJ4k=";
};
checkPhase = ":";
cargoLock.lockFile = "${src}/Cargo.lock";
};
nix-git = writeShellScriptBin "nix-git-commit" ''
echo ${self.rev or self.dirtyRev or "dirty"}
'';

native = {
win = rec {
passthru = {
inherit xwin nix-git;
splatted = runCommand "splat"
{
nativeBuildInputs = [ passthru.xwin ];
outputHashMode = "recursive";
outputHashAlgo = "sha256";
outputHash = "sha256-5ZZeEBuemx+lRmc9PczMfd13JwTvI6qMNvNmHdtK+1U=";
}
''
mkdir -p $out/xwin
xwin --accept-license --manifest ${./.msvc_manifest.json} splat --output $out/xwin --copy
'';

# wrap cargo by checking and injecting xwin where it is needed
rust = (runCommandLocal "cargo-xwinize" { } ''
cp -r ${rustOrig} $out
chmod +w $out/bin
mv $out/bin/cargo $out/bin/.cargo
cat << EOF > $out/bin/cargo
#!${runtimeShell}
case "\$1" in
build|check|clippy|run|rustc|test)
# replace linux target with msvc
if [ -z "\$CARGO_ENCODED_RUSTFLAGS" ]; then
args=()
prev_was_target=
for i in "\$@"; do
if [ -n "\$prev_was_target" ]; then
args+=("${CARGO_BUILD_TARGET}");
prev_was_target=
continue
fi
case "\$i" in
--target)
prev_was_target=1
;;
--)
# duplicate '--' to satisfy xwin test
if [ "\$1" = "test" ]; then
args+=("\$i");
fi
;;
esac
args+=("\$i");
done
exec -a "\$0" $out/bin/.cargo xwin "\''${args[@]}"
fi
;;
esac
exec -a "\$0" $out/bin/.cargo "\$@"
EOF
chmod +x $out/bin/cargo
'')
//
{ inherit (rustOrig) meta; };
rustPlatform = makeRustPlatform { cargo = passthru.rust; rustc = passthru.rust; };

setupXWin = topDir: /* bash */ ''
if [ ! -d "${topDir}" ]; then
mkdir -p "${topDir}"
fi
export WINEPREFIX="${topDir}/.wine"
export XWIN_ARCH="x86_64"
export XWIN_CACHE_DIR="${topDir}/.xwin"
if [ ! -d $XWIN_CACHE_DIR ]; then
mkdir -p $XWIN_CACHE_DIR
cp -r ${passthru.splatted}/xwin $XWIN_CACHE_DIR
chmod -R +w $XWIN_CACHE_DIR
echo "x86_64" > $XWIN_CACHE_DIR/xwin/DONE
fi
'';
};

auditable = false; # disable cargo auditable

CARGO_BUILD_TARGET = "x86_64-pc-windows-msvc";
CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_RUNNER = writeShellScript "wine-wsl" ''
if ! command -v /bin/wslpath &> /dev/null; then
${wine64}/bin/wine64 "$@"
else
"$@"
fi
'';

# On Windows MSVC, statically link the C runtime so that the resulting EXE does
# not depend on the vcruntime DLL.
RUSTFLAGS = "-C target-feature=+crt-static";

nativeBuildInputs = [
llvmPackages.llvm
llvmPackages.bintools
llvmPackages.clang
llvmPackages.lld
cargo-xwin
nix-git
pandoc
];

preConfigure = passthru.setupXWin "$(mktemp -d)";

installPhase = ''
mkdir -p $out/bin
cp target/${CARGO_BUILD_TARGET}/*/*.exe $out/bin
'';

CODCHI_WSL_VERSION_MIN = "2.0.14";
CODCHI_WSL_VERSION_MAX = "2.3.26";

};
linux = rec {
CARGO_BUILD_TARGET = "x86_64-unknown-linux-gnu";

CODCHI_LXD_CONTAINER_STORE = store-lxd-tarball;
CODCHI_LXD_CONTAINER_MACHINE = machine-lxd-tarball;

passthru = {
inherit xwin nix-git;
rust = rustOrig;
rustPlatform = rustPlatformOrig;
};
nativeBuildInputs = [
pkg-config
nix-git
makeWrapper
pandoc
installShellFiles
];
buildInputs = [
gtk3
libayatana-appindicator.out
];

outputs = [ "out" "docs" ];

postInstall = ''
installManPage ./target/codchi/man/*
installShellCompletion --cmd codchi \
--bash ./target/codchi/completions/codchi.bash \
--fish ./target/codchi/completions/codchi.fish \
--zsh ./target/codchi/completions/_codchi
mkdir -p $docs
cp -r ./target/codchi/md $docs/usage
'';

postFixup = ''
patchelf "$out/bin/codchi" \
--add-rpath ${lib.makeLibraryPath buildInputs}
wrapProgram "$out/bin/codchi" \
--set CODCHI_LXD_CONTAINER_STORE $CODCHI_LXD_CONTAINER_STORE \
--set CODCHI_LXD_CONTAINER_MACHINE $CODCHI_LXD_CONTAINER_MACHINE
'';
};
}.${platform};


in
native.passthru.rustPlatform.buildRustPackage (lib.recursiveUpdate args native)
9 changes: 8 additions & 1 deletion build/msix/AppxManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@
<uap5:ExecutionAlias Alias="codchi.exe" />
</uap5:AppExecutionAlias>
</uap5:Extension>
<desktop:Extension Category="windows.startupTask" Executable="codchi.exe" EntryPoint="Windows.FullTrustApplication">
</Extensions>
</Application>
<Application Id="Codchi.Gui" Executable="codchiw.exe" EntryPoint="Windows.FullTrustApplication">
<uap:VisualElements DisplayName="Codchiw" Description="Codchi's launcher for GUI apps"
Square150x150Logo="Assets\favicon150.png" Square44x44Logo="Assets\favicon44.png"
BackgroundColor="transparent" AppListEntry="none"/>
<Extensions>
<desktop:Extension Category="windows.startupTask" Executable="codchiw.exe" EntryPoint="Windows.FullTrustApplication">
<desktop:StartupTask TaskId="codchi-tray" Enabled="true" DisplayName="Codchi Tray Icon" />
</desktop:Extension>
</Extensions>
Expand Down
Loading

0 comments on commit dd2232e

Please sign in to comment.