Skip to content

Commit

Permalink
Flake support, project reorganization, asahi overlay (#47)
Browse files Browse the repository at this point in the history
* add flake.nix

* remove default.nix and pins.nix

* reorganize the project into `packages`, `nixos-module`, and `installer-bootstrap`
note: installer-bootstrap won't build at this commit

* fix installer-bootstrap to fit the new project structure

* remove the distinction between installer-bootstrap and installer-bootstrap-cross

* use nixpkgs `lib.nixosSystem` to build the installer
adjust project structure

* make the nixos module and installer use our overlay

* update flake.nix

* update package names

* improve flake.nix
export m1n1 and uboot-asahi for cross-compilation
remove asahi-fwextract from package outputs

* nixos-module: add overlay with mkBefore

* nixos-module: use `lib.findFirst` in the default firmware directory

* set up cross-compilation logic for pure evaluation
stop using `pkgsCross.aarch64-multiplatform`

* make pkgs more explicit and consistent

* remove nixpkgs re-imports

* flake.lock: Update

Flake lock file updates:

• Updated input 'nixpkgs':
    'github:nixos/nixpkgs/37b97ae3dd714de9a17923d004a2c5b5543dfa6d' (2023-01-13)
  → 'github:nixos/nixpkgs/2d38b664b4400335086a713a0036aafaa002c003' (2023-01-17)
• Updated input 'rust-overlay':
    'github:oxalica/rust-overlay/ca474ccdd5f81ed742328e15dae38bb57a1006e3' (2023-01-13)
  → 'github:oxalica/rust-overlay/5f7315b9800e2e500e6834767a57e39f7dbfd495' (2023-01-19)

* simplify flake dependencies

* reorganize nixos module
use `apple-silicon-support/modules` and `apple-silicon-support/packages`

* fix default peripheral firmware paths

* fix cross-compilation for packages

* fix mesa overlay
  • Loading branch information
oati authored Jan 31, 2023
1 parent fe4fff7 commit bd0c471
Show file tree
Hide file tree
Showing 25 changed files with 252 additions and 151 deletions.
7 changes: 7 additions & 0 deletions apple-silicon-support/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{ ... }:

{
imports = [
./modules/default.nix
];
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
{ config, pkgs, lib, ... }:
let
bootM1n1 = config.hardware.asahi.pkgs.callPackage ../m1n1 {
pkgs' = config.hardware.asahi.pkgs;

bootM1n1 = pkgs'.m1n1.override {
isRelease = true;
withTools = false;
customLogo = config.boot.m1n1CustomLogo;
};

bootUBoot = config.hardware.asahi.pkgs.callPackage ../u-boot {
bootUBoot = pkgs'.uboot-asahi.override {
m1n1 = bootM1n1;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@
./boot-m1n1
];

config = {
hardware.asahi.pkgs = if config.hardware.asahi.pkgsSystem != "aarch64-linux"
then import (pkgs.path) {
system = config.hardware.asahi.pkgsSystem;
crossSystem.system = "aarch64-linux";
}
else pkgs;
};
config =
let
cfg = config.hardware.asahi;
in {
nixpkgs.overlays = lib.mkBefore [ cfg.overlay ];

hardware.asahi.pkgs =
if cfg.pkgsSystem != "aarch64-linux"
then
import (pkgs.path) {
crossSystem.system = "aarch64-linux";
localSystem.system = cfg.pkgsSystem;
overlays = [ cfg.overlay ];
}
else pkgs;
};

options.hardware.asahi = {
pkgsSystem = lib.mkOption {
Expand All @@ -35,5 +43,19 @@
with the system defined by `hardware.asahi.pkgsSystem`.
'';
};

overlay = lib.mkOption {
type = lib.mkOptionType {
name = "nixpkgs-overlay";
description = "nixpkgs overlay";
check = lib.isFunction;
merge = lib.mergeOneOption;
};
default = import ../packages/overlay.nix;
defaultText = "overlay provided with the module";
description = ''
The nixpkgs overlay for asahi packages.
'';
};
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
{ config, pkgs, lib, ... }:
{
config = {
boot.kernelPackages = config.hardware.asahi.pkgs.callPackage ./package.nix {
inherit (config.boot) kernelPatches;
_4KBuild = config.hardware.asahi.use4KPages;
withRust = config.hardware.asahi.withRust;
};
boot.kernelPackages = let
pkgs' = config.hardware.asahi.pkgs;
in
pkgs'.linux-asahi.override {
inherit (config.boot) kernelPatches;
_4KBuild = config.hardware.asahi.use4KPages;
withRust = config.hardware.asahi.withRust;
};

# we definitely want to use CONFIG_ENERGY_MODEL, and
# schedutil is a prerequisite for using it
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@
&& config.hardware.asahi.experimentalGPUInstallMode == mode);
in lib.mkMerge [
(lib.mkIf config.hardware.asahi.useExperimentalGPUDriver {
# make the Asahi Mesa available via an overlay so the user has access to it
# (being careful not to create infinite recursion if the user wants to overlay
# it over the original Mesa)
nixpkgs.overlays = [
(final: prev: {
mesa-asahi-edge = final.callPackage ./package.nix { inherit (prev) mesa; };
})
];

# install the drivers
hardware.opengl.package = pkgs.mesa-asahi-edge.drivers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,25 @@
];

hardware.firmware = let
asahi-fwextract = pkgs.callPackage ../asahi-fwextract {};
in lib.mkIf ((config.hardware.asahi.peripheralFirmwareDirectory != null)
&& config.hardware.asahi.extractPeripheralFirmware) [
(pkgs.stdenv.mkDerivation {
name = "asahi-peripheral-firmware";
pkgs' = config.hardware.asahi.pkgs;
in
lib.mkIf ((config.hardware.asahi.peripheralFirmwareDirectory != null)
&& config.hardware.asahi.extractPeripheralFirmware) [
(pkgs.stdenv.mkDerivation {
name = "asahi-peripheral-firmware";

nativeBuildInputs = [ asahi-fwextract pkgs.cpio ];
nativeBuildInputs = [ pkgs'.asahi-fwextract pkgs.cpio ];

buildCommand = ''
mkdir extracted
asahi-fwextract ${config.hardware.asahi.peripheralFirmwareDirectory} extracted
buildCommand = ''
mkdir extracted
asahi-fwextract ${config.hardware.asahi.peripheralFirmwareDirectory} extracted
mkdir -p $out/lib/firmware
cat extracted/firmware.cpio | cpio -id --quiet --no-absolute-filenames
mv vendorfw/* $out/lib/firmware
'';
})
];
mkdir -p $out/lib/firmware
cat extracted/firmware.cpio | cpio -id --quiet --no-absolute-filenames
mv vendorfw/* $out/lib/firmware
'';
})
];
};

options.hardware.asahi = {
Expand All @@ -43,20 +44,15 @@

peripheralFirmwareDirectory = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = let
paths = [

default = lib.findFirst (path: builtins.pathExists (path + "/all_firmware.tar.gz")) null
[
# path when the system is operating normally
"/boot/asahi"
/boot/asahi
# path when the system is mounted in the installer
"/mnt/boot/asahi"
/mnt/boot/asahi
];

validPaths = (builtins.filter
(p: builtins.pathExists (p + "/all_firmware.tar.gz"))
paths) ++ [ null ];

firstPath = builtins.elemAt validPaths 0;
in if firstPath != null then "${/. + firstPath}" else null;
description = ''
Path to the directory containing the non-free non-redistributable
peripheral firmware necessary for features like Wi-Fi. Ordinarily, this
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
{ pkgs, _4KBuild ? false, withRust ? false, kernelPatches ? [ ] }: let
localPkgs =
# we do this so the config can be read on any system and not affect
# the output hash
if builtins ? currentSystem then import (pkgs.path) { system = builtins.currentSystem; }
{ lib
, pkgs
, callPackage
, writeShellScriptBin
, writeText
, linuxPackagesFor
, _4KBuild ? false
, withRust ? false
, kernelPatches ? [ ]
}:

let
# TODO: use a pure nix regex parser instead of an IFD, and remove this workaround
localPkgs = if builtins ? currentSystem
then import (pkgs.path) {
crossSystem.system = builtins.currentSystem;
localSystem.system = builtins.currentSystem;
}
else pkgs;

lib = localPkgs.lib;
inherit (localPkgs) runCommand;

parseExtraConfig = cfg: let
lines = builtins.filter (s: s != "") (lib.strings.splitString "\n" cfg);
Expand All @@ -15,7 +28,7 @@
"CONFIG_${builtins.elemAt kv 0}=${builtins.elemAt kv 1}";
in lib.strings.concatMapStringsSep "\n" perLine lines;

readConfig = configfile: import (localPkgs.runCommand "config.nix" { } ''
readConfig = configfile: import (runCommand "config.nix" { } ''
echo "{ } // " > "$out"
while IFS='=' read key val; do
[ "x''${key#CONFIG_}" != "x$key" ] || continue
Expand All @@ -25,16 +38,16 @@
echo "{ }" >> $out
'').outPath;

linux_asahi_pkg = { stdenv, lib, fetchFromGitHub, fetchpatch, linuxKernel,
linux-asahi-pkg = { stdenv, lib, fetchFromGitHub, fetchpatch, linuxKernel,
rustPlatform, rustfmt, rust-bindgen, ... } @ args:
let
configfile = if kernelPatches == [ ] then ./config else
pkgs.writeText "config" ''
${builtins.readFile ./config}
writeText "config" ''
${builtins.readFile ./config}
# Patches
${lib.strings.concatMapStringsSep "\n" ({extraConfig ? "", ...}: parseExtraConfig extraConfig) kernelPatches}
'';
# Patches
${lib.strings.concatMapStringsSep "\n" ({extraConfig ? "", ...}: parseExtraConfig extraConfig) kernelPatches}
'';

_kernelPatches = kernelPatches;
in
Expand Down Expand Up @@ -82,7 +95,7 @@
# is running, so we give the kernel build a rustc that wraps the real rustc
# while setting the appropriate environment variable during its execution.
# https://github.com/NixOS/nixpkgs/pull/209113
(pkgs.writeShellScriptBin "rustc" ''
(writeShellScriptBin "rustc" ''
NIX_LDFLAGS=-lgcc ${rustPlatform.rust.rustc}/bin/rustc "$@"
'')
];
Expand All @@ -96,6 +109,6 @@
'';
} else {});

linux_asahi = (pkgs.callPackage linux_asahi_pkg { });
in pkgs.recurseIntoAttrs (pkgs.linuxPackagesFor linux_asahi)
linux-asahi = (callPackage linux-asahi-pkg { });
in lib.recurseIntoAttrs (linuxPackagesFor linux-asahi)

File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{ stdenv
, buildPackages
, lib
, fetchFromGitHub
, pkgsCross
, python3
, dtc
, imagemagick
Expand Down Expand Up @@ -36,13 +36,13 @@ in stdenv.mkDerivation rec {
fetchSubmodules = true;
};

makeFlags = [ "ARCH=aarch64-unknown-linux-gnu-" ]
makeFlags = [ "ARCH=${stdenv.cc.targetPrefix}" ]
++ lib.optional isRelease "RELEASE=1"
++ lib.optional withChainloading "CHAINLOADING=1";

nativeBuildInputs = [
dtc
pkgsCross.aarch64-multiplatform.buildPackages.gcc
buildPackages.gcc
] ++ lib.optional withChainloading rustenv
++ lib.optional (customLogo != null) imagemagick;

Expand Down Expand Up @@ -86,14 +86,14 @@ EOF
chmod +x $script
done
GCC=${pkgsCross.aarch64-multiplatform.buildPackages.gcc}
BINUTILS=${pkgsCross.aarch64-multiplatform.buildPackages.binutils-unwrapped}
GCC=${buildPackages.gcc}
BINUTILS=${buildPackages.binutils-unwrapped}
ln -s $GCC/bin/*-gcc $out/toolchain-bin/
ln -s $GCC/bin/*-ld $out/toolchain-bin/
ln -s $BINUTILS/bin/*-objcopy $out/toolchain-bin/
ln -s $BINUTILS/bin/*-objdump $out/toolchain-bin/
ln -s $GCC/bin/*-nm $out/toolchain-bin/
ln -s $GCC/bin/${stdenv.cc.targetPrefix}gcc $out/toolchain-bin/
ln -s $GCC/bin/${stdenv.cc.targetPrefix}ld $out/toolchain-bin/
ln -s $BINUTILS/bin/${stdenv.cc.targetPrefix}objcopy $out/toolchain-bin/
ln -s $BINUTILS/bin/${stdenv.cc.targetPrefix}objdump $out/toolchain-bin/
ln -s $GCC/bin/${stdenv.cc.targetPrefix}nm $out/toolchain-bin/
'') + ''
runHook postInstall
'';
Expand Down
File renamed without changes.
8 changes: 8 additions & 0 deletions apple-silicon-support/packages/overlay.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
final: prev: {
linux-asahi = final.callPackage ./linux-asahi { };
m1n1 = final.callPackage ./m1n1 { };
uboot-asahi = final.callPackage ./uboot-asahi { };
asahi-fwextract = final.callPackage ./asahi-fwextract { };
mesa-asahi-edge = final.callPackage ./mesa-asahi-edge { inherit (prev) mesa; };
# TODO: package alsa-ucm-conf-asahi for headphone jack support
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
{ lib
, fetchFromGitHub
, fetchpatch
, pkgs
, pkgsCross
, buildUBoot
, m1n1
}: let
# u-boot's buildInputs get a different hash and don't build right if we try to
# cross-build for aarch64 on itself for whatever reason
buildPkgs = if pkgs.stdenv.system == "aarch64-linux" then pkgs else pkgsCross.aarch64-multiplatform;
in (buildPkgs.buildUBoot rec {
}:

(buildUBoot rec {
src = fetchFromGitHub {
# tracking: https://github.com/AsahiLinux/PKGBUILDs/blob/stable/uboot-asahi/PKGBUILD
owner = "AsahiLinux";
Expand Down
9 changes: 0 additions & 9 deletions default.nix

This file was deleted.

44 changes: 44 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit bd0c471

Please sign in to comment.