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

darling: init at unstable-2023-05-02 #227765

Merged
merged 4 commits into from May 7, 2023
Merged

darling: init at unstable-2023-05-02 #227765

merged 4 commits into from May 7, 2023

Conversation

zhaofengli
Copy link
Member

@zhaofengli zhaofengli commented Apr 23, 2023

Description of changes

This PR adds Darling which is a Darwin/macOS emulation layer for Linux.

Patches applied:

Fixes #38628.

Installing

Set programs.darling.enable = true; in your NixOS config. This will install the setuid wrapper necessary to start Darling. Then, run darling shell.

Stuff To Try

See also https://docs.darlinghq.org/what-to-try.html

$ uname -a
Darwin host 19.6.0 Darwin Kernel Version 19.6.0 x86_64

# You can also use a real copy of Xcode (I haven't tried)
$ xcode-select --install
[...]

# Compile some code
$ clang hello.c

# Install Nix (does not fully work yet - more below)
$ curl -LO "https://github.com/zhaofengli/nix-on-darling/releases/download/initial-hack/nixos-system-x86_64-darwin.tar.xz"
[...]
$ tar xpf nixos-system-x86_64-darwin.tar.xz -C /
[...]
$ bootstrap-nix
Bootstrapping Nix...
installing 'nix-2.13.3'
building '/nix/store/ja3kqp1qwz66iqbkg50wzj264rppcnmg-user-environment.drv'...
installing 'nss-cacert-3.86'
building '/nix/store/27fw1kpgngj3q29ndphb30dw1vkhaxdz-user-environment.drv'...
Run the following command to make Nix work in the current shell:
. '/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh'

$ . /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh

$ nix-instantiate --eval -E 'builtins.currentSystem'
"x86_64-darwin"

# Try other stuff
$ nix run nixpkgs#neofetch
                    c.'          zhaofeng@host 
                 ,xNMM.          -------------- 
               .OMMMMo           OS: macOS Catalina 10.15 Darling x86_64   
               lMM"              Kernel: 19.6.0 
     .;loddo:.  .olloddol;.      Uptime: 86 days, 4 hours, 9 mins 
   cKMMMMMMMMMMNWMMMMMMMMMM0:    Shell: fish /nix/store/jawzvjj5x2iwv8zmz0905ix2v62a9h3r-neofetch- 
 .KMMMMMMMMMMMMMMMMMMMMMMMWd.    DE: Aqua 
 XMMMMMMMMMMMMMMMMMMMMMMMX.      WM: Quartz Compositor 
;MMMMMMMMMMMMMMMMMMMMMMMM:       WM Theme: Blue (Print: Entry, AppleInterfaceStyle, Does Not Exist 
:MMMMMMMMMMMMMMMMMMMMMMMM:       Terminal: shellspawn 
.MMMMMMMMMMMMMMMMMMMMMMMMX.      Memory: 27141MiB / 32034MiB 
 kMMMMMMMMMMMMMMMMMMMMMMMMWd.
 'XMMMMMMMMMMMMMMMMMMMMMMMMMMk                           
  'XMMMMMMMMMMMMMMMMMMMMMMMMK.                           
    kMMMMMMMMMMMMMMMMMMMMMMd
     ;KMMMMMMMWXXWMMMMMMMk.
       "cooc*"    "*coo'"

Nix

The official Nix install script does not work on Darling due to its attempts to modify partitions and users on the system. Nix also has trouble clearing file attributes in the emulated filesystem.

Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 23.05 Release Notes (or backporting 22.11 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

@zhaofengli zhaofengli force-pushed the darling branch 2 times, most recently from 7537f60 to 3058b3d Compare April 23, 2023 19:39
pkgs/applications/emulators/darling/default.nix Outdated Show resolved Hide resolved
pkgs/applications/emulators/darling/default.nix Outdated Show resolved Hide resolved
pkgs/applications/emulators/darling/default.nix Outdated Show resolved Hide resolved
@zhaofengli
Copy link
Member Author

The previous patchShebangs . was too indiscriminate and caused scripts intended to be run inside the Darling sandbox to be patched as well. Also added a check to make sure that the Darling sysroot doesn't contain references to the Nix store (except for mldr).

@zhaofengli zhaofengli changed the title darling: init at unstable-2023-04-21 darling: init at unstable-2023-04-26 Apr 26, 2023
nixos/modules/programs/darling.nix Outdated Show resolved Hide resolved
nixos/modules/programs/darling.nix Outdated Show resolved Hide resolved
@zhaofengli zhaofengli force-pushed the darling branch 2 times, most recently from 78ff73c to 61a2046 Compare May 1, 2023 23:39
@zhaofengli
Copy link
Member Author

During the build, Darling produces a source-built SDK which is now installed into a separate output. This SDK can be used to cross-compile from Linux to macOS (see the NixOS test). I tried the resulting executable and it worked fine on a real M1 Mac mini under Rosetta 2.

Very hacky cross stdenv
let
  lib = import ./lib;

  pkgs = import ./. {
    crossSystem = lib.systems.elaborate (lib.systems.examples.x86_64-darwin // {
      darwinSdkVersion = "10.15";
      darwinMinVersion = "10.15";
    });

    overlays = [ (final: prev: {
      stdenv =
        if (prev.stdenv.hostPlatform == prev.stdenv.buildPlatform) then prev.stdenv
        else prev.stdenv.override {
          overrides = overrideStdenv;
        };
    }) ];
  };

  overrideStdenv = final: prev: let
    nativePkgs = prev.pkgsBuildBuild;

    llvm = nativePkgs.llvmPackages_latest;
    sdk = "${nativePkgs.darling.sdk}/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk";

    nativeStuff = {
      nativeTools = false;
      nativeLibc = false;
      nativePrefix = "";
      shell = nativePkgs.stdenvNoCC.shell;
      inherit (nativePkgs) coreutils gnugrep;
    };

    mergedBintools = nativePkgs.symlinkJoin {
      name = "bintools-unwrapped";
      paths = [ llvm.bintools-unwrapped ];
      postBuild = ''
        ln -sf ${nativePkgs.darling.sdk}/bin/x86_64-apple-darwin19-ld $out/bin/ld
      '';
    };

    bintools = prev.wrapBintoolsWith ({
      bintools = mergedBintools;
      libc = "${sdk}/usr";
    } // nativeStuff);

    cc = prev.wrapCCWith (rec {
      cc = llvm.clang-unwrapped;
      inherit bintools;
      libc = "${sdk}/usr";
      libcxx = null;

      isClang = true;

      gccForLibs = null;
      extraBuildCommands = ''
        echo "--sysroot=${sdk}" >> $out/nix-support/cc-cflags

        rsrc="$out/resource-root"
        mkdir "$rsrc"
        ln -s "${cc.lib or cc}/lib/clang/${cc.version}/include" "$rsrc"
        echo "-resource-dir=$rsrc" >> $out/nix-support/cc-cflags
      '';
    } // nativeStuff);

    stdenv = prev.stdenv.override {
      inherit cc;
      extraBuildInputs = [];
    };
  in {
    inherit stdenv;
  };
in pkgs

@zhaofengli zhaofengli changed the title darling: init at unstable-2023-04-26 darling: init at unstable-2023-05-01 May 1, 2023
@zhaofengli
Copy link
Member Author

@ofborg test darling

nixos/modules/programs/darling.nix Outdated Show resolved Hide resolved
nixos/modules/programs/darling.nix Outdated Show resolved Hide resolved
# darling-dmg
fuse

# CoreAudio
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

??

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are asking about the comments, these are supposed to indicate which components the wrapped libraries are used in. I'll make that clearer.

@zhaofengli zhaofengli changed the title darling: init at unstable-2023-05-01 darling: init at unstable-2023-05-02 May 7, 2023
@Kranzes Kranzes merged commit 6a751fe into NixOS:master May 7, 2023
22 checks passed
@zhaofengli zhaofengli deleted the darling branch May 7, 2023 21:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Package darling
5 participants