+async function patchelf(dest: PathLike): Promise<void> {
+ await vscode.window.withProgress(
+ {
+ location: vscode.ProgressLocation.Notification,
+ title: "Patching rust-analyzer for NixOS"
+ },
+ async (progress, _) => {
+ const expression = `
+ {src, pkgs ? import <nixpkgs> {}}:
+ pkgs.stdenv.mkDerivation {
+ name = "rust-analyzer";
+ inherit src;
+ phases = [ "installPhase" "fixupPhase" ];
+ installPhase = "cp $src $out";
+ fixupPhase = ''
+ chmod 755 $out
+ patchelf --set-interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" $out
+ '';
+ }
+ `;
+ const origFile = dest + "-orig";
+ await fs.rename(dest, origFile);
+ progress.report({ message: "Patching executable", increment: 20 });
+ await new Promise((resolve, reject) => {
+ const handle = exec(`nix-build -E - --arg src '${origFile}' -o ${dest}`,
+ (err, stdout, stderr) => {
+ if (err != null) {
+ reject(Error(stderr));
+ } else {
+ resolve(stdout);
+ }
+ });
+ handle.stdin?.write(expression);
+ handle.stdin?.end();
+ });
+ await fs.unlink(origFile);
+ }
+ );
+}
+