tpwrules / nixos-apple-silicon

Resources to install NixOS bare metal on Apple Silicon Macs
MIT License
743 stars 73 forks source link

`zig build` always panics #115

Open Treeniks opened 8 months ago

Treeniks commented 8 months ago

I'm unsure how to properly diagnose this issue, but since it didn't happen on Asahi Fedora (at least I don't think it did, I switched to NixOS so I don't have Fedora installed anymore to test), I assume it happens because of something related to Nix.

When I try to compile any project with Zig, I always get the following error:

$ mkdir test
$ cd test
$ zig init-exe
$ zig build
thread 2800 panic: reached unreachable code
Panicked during a panic. Aborting.
error: the following build command crashed:
/home/suteki/Documents/test/zig-cache/o/5e10d47150793eea1c581cd99721be79/build /nix/store/kvsk07n6bxdzzznl48ksdqnajnry1jkh-zig-0.11.0/bin/zig /home/suteki/Documents/test /home/suteki/Documents/test/zig-cache /home/suteki/.cache/zig run

Using zig build-exe works fine, it's only zig build with a build.zig that does not work. I tried using the zig binary installed by the zig Nix package, as well as one downloaded from the Zig website. I also tried compiling the bootstrap compiler myself, but it unfortunately had the same result.

psanford commented 8 months ago

FWIW, your instructions work fine for me with zig installed from nixpkgs-23.05 on my m2 air. Is there any error in dmesg?

Treeniks commented 8 months ago

Very strange, I'm also running an M2 Air.

dmesg gave me these errors:

[    0.359054] macsmc-rtkit 23e400000.smc: Failed to create device link (0x180) with 23d714000.spmi:pmu@e:rtc_nvmem@f800
[    0.360306] macsmc-rtkit 23e400000.smc: Failed to create device link (0x180) with 23d714000.spmi:pmu@e:rtc_nvmem@f800
[    0.361553] macsmc-rtkit 23e400000.smc: Failed to create device link (0x180) with 23d714000.spmi:pmu@e:legacy_nvmem@f700
[    1.152425] apple-dcp 231c00000.dcp: dcp_poweron() starting
[    1.436563] pcie-apple 690000000.pcie: invalid resource (null)
[    3.510231] ieee80211 phy0: brcmf_p2p_set_firmware: failed to update device address ret -52
[    3.513002] ieee80211 phy0: brcmf_p2p_create_p2pdev: set p2p_disc error
[    3.514245] ieee80211 phy0: brcmf_cfg80211_add_iface: add iface p2p-dev-wlp1s0f type 10 failed: err=-52

All of which were there before running zig.

I doubt it's related, but I found my generation selection on boot to look wrong as well, maybe there is something borked with my install? IMG_20231101_182822492

This is my current configuration.nix:

{ config, lib, pkgs, ... }:

{
  imports =
      /etc/nixos/hardware-configuration.nix
      /etc/nixos/apple-silicon-support
    ];

  boot.extraModprobeConfig = ''
    options hid_apple swap_fn_leftctrl=1
    options hid_apple swap_opt_cmd=1
    options hid_apple fnmode=2
  '';

  boot.loader.systemd-boot.enable = true;
  boot.loader.efi.canTouchEfiVariables = false;

  hardware.asahi.peripheralFirmwareDirectory = /etc/nixos/firmware;
  hardware.asahi.useExperimentalGPUDriver = true;

  networking.hostName = "houjicha";
  networking.networkmanager.enable = true;

  time.timeZone = "Europe/Berlin";
  services.ntp.enable = true;

  services.xserver.enable = true;
  # Display Manager
  services.greetd = {
    enable = true;
    settings = {
      default_session = {
        command = "${pkgs.greetd.tuigreet}/bin/tuigreet --time --cmd Hyprland --asterisks --user-menu";
        user = "greeter";
      };
    };
  };
  # Window Manager
  programs.hyprland.enable = true;
  programs.waybar.enable = true;
  services.xserver.displayManager.sessionPackages = with pkgs; [
    river
  ];
  xdg.portal.enable = true;
  xdg.portal.wlr.enable = true;
  xdg.portal.extraPortals = with pkgs; [
    xdg-desktop-portal-gtk
  ];
  environment.sessionVariables = rec {
    XDG_CACHE_HOME  = "$HOME/.cache";
    XDG_CONFIG_HOME = "$HOME/.config";
    XDG_DATA_HOME   = "$HOME/.local/share";
    XDG_STATE_HOME  = "$HOME/.local/state";
    XDG_BIN_HOME    = "$HOME/.local/bin";
    PATH = [
      "${XDG_BIN_HOME}"
    ];
  };

  # Enable touchpad support
  services.xserver.libinput.enable = true;

  users.users.thomas = {
    isNormalUser = true;
    extraGroups = [ "video" "networkmanager" "wheel" ];
    packages = with pkgs; [];
    shell = pkgs.fish;
  };

  environment.systemPackages = with pkgs; [
    # CLI
    gnumake
    just
    git
    wget
    vim
    neovim
    helix
    gcc
    gdb
    clang
    rustup
    julia
    zig
    zls
    texlive.combined.scheme-full
    (python3.withPackages (ps: [ps.dbus-python ps.pygments]))

    # GUI
    wezterm
    alacritty
    firefox
    gnome.gnome-tweaks
    sublime4
    sublime-merge
    vscode
    inkscape
    pcmanfm
    lxappearance

    # THEMES
    numix-cursor-theme
    papirus-icon-theme
    rose-pine-gtk-theme
    rose-pine-icon-theme

    # MISC
    greetd.tuigreet
    river
    wofi
    hyprpaper
    swaylock-effects
    isabelle
    wl-clipboard
    pinentry
    pinentry-gnome
    dbus
  ];

  fonts.packages = with pkgs; [
    (nerdfonts.override { fonts = [ "JetBrainsMono" "FiraCode" ]; })
    dejavu_fonts
    jetbrains-mono
    fira-code
    roboto
    roboto-slab
    roboto-mono
    roboto-serif
  ];
  fonts.fontDir.enable = true;

  programs.gnupg.agent = {
    enable = true;
    enableSSHSupport = true;
    pinentryFlavor = "gnome3";
  };
  programs.light.enable = true;
  programs.fish.enable = true;

  nix.settings.experimental-features = [ "nix-command" "flakes" ];

  nixpkgs.config.allowUnfree = true;
  nixpkgs.config.permittedInsecurePackages = [
    # required for sublime4
    "openssl-1.1.1w"
  ];

  system.stateVersion = "23.11";
}
tpwrules commented 7 months ago

The bootloader looking wrong is normal and unrelated. So are the dmesg entries.

I can replicate your issue on my M1 Max using the store path you used. This looks like it's something within Zig itself, or some interaction with nixpkgs. 23.05 has Zig 0.10.1 and using that on nixpkgs master (zig_0_10) works while Zig 0.11.0 (zig) crashes.

I can't really offer any further assistance debugging and it seems unrelated to Apple Silicon specifically.

tpwrules commented 7 months ago

This does not crash on a VM under macOS, nor on another aarch64 board. I'm guessing it's a page size issue then.

I'll keep this open since it's at least Apple Silicon induced but I don't have resources to debug this further.

RossComputerGuy commented 5 months ago

Hey so I am actually the developer of the runtime page size PR for Zig and @tpwrules is right that it is indeed this issue. My PR is nearing completion and I expect to have it in by Zig 0.12 is released.

PR: https://github.com/ziglang/zig/pull/17382

Fork: https://github.com/ExpidusOS/zig/tree/fix/linux-page-size