commit df8c1f5256cc49f5855fb5cbd8630c6b98cdde2c Author: Adam Stephens Date: Mon Jan 22 12:47:25 2024 -0500 init: x13s module and packages diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..b1d4c8b --- /dev/null +++ b/.envrc @@ -0,0 +1,5 @@ +use flake + +if [ -e .secret.envrc ]; then + source .secret.envrc +fi diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..af8f4e4 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.secret.envrc diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..d7dd009 --- /dev/null +++ b/flake.lock @@ -0,0 +1,64 @@ +{ + "nodes": { + "flake-parts": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib" + }, + "locked": { + "lastModified": 1704982712, + "narHash": "sha256-2Ptt+9h8dczgle2Oo6z5ni5rt/uLMG47UFTR1ry/wgg=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "07f6395285469419cf9d078f59b5b49993198c00", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1705856552, + "narHash": "sha256-JXfnuEf5Yd6bhMs/uvM67/joxYKoysyE3M2k6T3eWbg=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "612f97239e2cc474c13c9dafa0df378058c5ad8d", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-lib": { + "locked": { + "dir": "lib", + "lastModified": 1703961334, + "narHash": "sha256-M1mV/Cq+pgjk0rt6VxoyyD+O8cOUiai8t9Q6Yyq4noY=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "b0d36bd0a420ecee3bc916c91886caca87c894e9", + "type": "github" + }, + "original": { + "dir": "lib", + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-parts": "flake-parts", + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..c44040e --- /dev/null +++ b/flake.nix @@ -0,0 +1,25 @@ +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + flake-parts.url = "github:hercules-ci/flake-parts"; + }; + + outputs = + inputs@{ flake-parts, ... }: + flake-parts.lib.mkFlake { inherit inputs; } { + imports = [ ./packages/part.nix ]; + + systems = [ + "x86_64-linux" + "aarch64-linux" + ]; + + perSystem = + { pkgs, ... }: + { + devShells.default = pkgs.mkShellNoCC { packages = [ pkgs.npins ]; }; + }; + + flake.nixosModules.default = import ./module.nix; + }; +} diff --git a/module.nix b/module.nix new file mode 100644 index 0000000..bd90550 --- /dev/null +++ b/module.nix @@ -0,0 +1,123 @@ +{ + config, + lib, + pkgs, + ... +}: +let + cfg = config.nixos-x13s; + + x13sPackages = import ./packages/default.nix { inherit lib pkgs; }; + + dtbName = "sc8280xp-lenovo-thinkpad-x13s.dtb"; + linuxPackages_x13s = pkgs.linuxPackagesFor x13sPackages."x13s/linux"; + dtb = "${linuxPackages_x13s.kernel}/dtbs/qcom/${dtbName}"; + + alsa-ucm-conf-env.ALSA_CONFIG_UCM2 = "${x13sPackages."x13s/alsa-ucm-conf"}/share/alsa/ucm2"; +in +{ + options.nixos-x13s = { + enable = lib.mkEnableOption "x13s hardware support"; + + bluetoothMac = lib.mkOption { + type = lib.types.str; + description = "mac address to set on boot"; + }; + }; + + config = lib.mkIf cfg.enable { + environment.systemPackages = [ pkgs.efibootmgr ]; + + hardware.enableAllFirmware = true; + hardware.firmware = [ x13sPackages."x13s/extra-firmware" ]; + + systemd.services.pd-mapper = { + wantedBy = [ "multi-user.target" ]; + + serviceConfig = { + ExecStart = "${lib.getExe x13sPackages.pd-mapper}"; + Restart = "always"; + }; + }; + + environment.sessionVariables = alsa-ucm-conf-env; + systemd.user.services.pipewire.environment = alsa-ucm-conf-env; + systemd.user.services.wireplumber.environment = alsa-ucm-conf-env; + + boot = { + loader.efi.canTouchEfiVariables = true; + loader.systemd-boot.enable = lib.mkDefault true; + loader.systemd-boot.extraFiles = { + "${dtbName}" = dtb; + }; + + supportedFilesystems = lib.mkForce [ + "ext4" + "btrfs" + "vfat" + ]; + + initrd.supportedFilesystems = lib.mkForce [ + "btrfs" + "vfat" + ]; + + kernelPackages = linuxPackages_x13s; + + kernelParams = [ + # jhovold recommended + "efi=noruntime" + "clk_ignore_unused" + "pd_ignore_unused" + "arm64.nopauth" + + # blacklist graphics in initrd so the firmware can load from disk + "rd.driver.blacklist=msm" + ]; + + initrd = { + includeDefaultModules = false; + + kernelModules = [ + "nvme" + "phy-qcom-qmp-pcie" + "pcie-qcom" + + "i2c-core" + "i2c-hid" + "i2c-hid-of" + "i2c-qcom-geni" + + "leds_qcom_lpg" + "pwm_bl" + "qrtr" + "pmic_glink_altmode" + "gpio_sbu_mux" + "phy-qcom-qmp-combo" + "gpucc_sc8280xp" + "dispcc_sc8280xp" + "phy_qcom_edp" + "panel-edp" + # "msm" + ]; + }; + }; + + # default is performance + powerManagement.cpuFreqGovernor = "ondemand"; + + systemd.services.bluetooth = { + serviceConfig = { + # disabled because btmgmt call hangs + # ExecStartPre = [ + # "" + # "${pkgs.util-linux}/bin/rfkill block bluetooth" + # "${pkgs.bluez5-experimental}/bin/btmgmt public-addr ${cfg.bluetoothMac}" + # "${pkgs.util-linux}/bin/rfkill unblock bluetooth" + # ]; + RestartSec = 5; + Restart = "on-failure"; + }; + }; + }; +} diff --git a/npins/default.nix b/npins/default.nix new file mode 100644 index 0000000..4a7c372 --- /dev/null +++ b/npins/default.nix @@ -0,0 +1,47 @@ +# Generated by npins. Do not modify; will be overwritten regularly +let + data = builtins.fromJSON (builtins.readFile ./sources.json); + version = data.version; + + mkSource = spec: + assert spec ? type; let + path = + if spec.type == "Git" then mkGitSource spec + else if spec.type == "GitRelease" then mkGitSource spec + else if spec.type == "PyPi" then mkPyPiSource spec + else if spec.type == "Channel" then mkChannelSource spec + else builtins.throw "Unknown source type ${spec.type}"; + in + spec // { outPath = path; }; + + mkGitSource = { repository, revision, url ? null, hash, ... }: + assert repository ? type; + # At the moment, either it is a plain git repository (which has an url), or it is a GitHub/GitLab repository + # In the latter case, there we will always be an url to the tarball + if url != null then + (builtins.fetchTarball { + inherit url; + sha256 = hash; # FIXME: check nix version & use SRI hashes + }) + else assert repository.type == "Git"; builtins.fetchGit { + url = repository.url; + rev = revision; + # hash = hash; + }; + + mkPyPiSource = { url, hash, ... }: + builtins.fetchurl { + inherit url; + sha256 = hash; + }; + + mkChannelSource = { url, hash, ... }: + builtins.fetchTarball { + inherit url; + sha256 = hash; + }; +in +if version == 3 then + builtins.mapAttrs (_: mkSource) data.pins +else + throw "Unsupported format version ${toString version} in sources.json. Try running `npins upgrade`" diff --git a/npins/sources.json b/npins/sources.json new file mode 100644 index 0000000..03a0681 --- /dev/null +++ b/npins/sources.json @@ -0,0 +1,29 @@ +{ + "pins": { + "alsa-ucm-conf": { + "type": "Git", + "repository": { + "type": "GitHub", + "owner": "alsa-project", + "repo": "alsa-ucm-conf" + }, + "branch": "master", + "revision": "e87dde51d68950537f92af955ad0633437cc419a", + "url": "https://github.com/alsa-project/alsa-ucm-conf/archive/e87dde51d68950537f92af955ad0633437cc419a.tar.gz", + "hash": "14q6h5h4b6kyywmxf1jq726yrv1bl9q6i2pnfbl1hm7r66vgnaip" + }, + "jhovold-linux": { + "type": "Git", + "repository": { + "type": "GitHub", + "owner": "jhovold", + "repo": "linux" + }, + "branch": "wip/sc8280xp-v6.7", + "revision": "bac95eabe6577faa2773cbe7e91c34fd17ab79a0", + "url": "https://github.com/jhovold/linux/archive/bac95eabe6577faa2773cbe7e91c34fd17ab79a0.tar.gz", + "hash": "1y8abwa506q9dchqvnaqbb8n3i7nj2jnaxcll1f1476qi0yswc1n" + } + }, + "version": 3 +} \ No newline at end of file diff --git a/packages/default.nix b/packages/default.nix new file mode 100644 index 0000000..478cb39 --- /dev/null +++ b/packages/default.nix @@ -0,0 +1,64 @@ +{ lib, pkgs, ... }: +let + sources = import ../npins; + kp = [ + { + name = "x13s-cfg"; + patch = null; + extraStructuredConfig = with lib.kernel; { + EFI_ARMSTUB_DTB_LOADER = lib.mkForce yes; + OF_OVERLAY = lib.mkForce yes; + BTRFS_FS = lib.mkForce yes; + BTRFS_FS_POSIX_ACL = lib.mkForce yes; + MEDIA_CONTROLLER = lib.mkForce yes; + SND_USB_AUDIO_USE_MEDIA_CONTROLLER = lib.mkForce yes; + SND_USB = lib.mkForce yes; + SND_USB_AUDIO = lib.mkForce module; + USB_XHCI_PCI = lib.mkForce module; + NO_HZ_FULL = lib.mkForce yes; + HZ_100 = lib.mkForce yes; + HZ_250 = lib.mkForce no; + DRM_AMDGPU = lib.mkForce no; + DRM_NOUVEAU = lib.mkForce no; + QCOM_TSENS = lib.mkForce yes; + NVMEM_QCOM_QFPROM = lib.mkForce yes; + ARM_QCOM_CPUFREQ_NVMEM = lib.mkForce yes; + VIRTIO_PCI = lib.mkForce module; + # forthcoming kernel work: QCOM_PD_MAPPER = lib.mkForce module; + }; + } + ]; + + linux_x13s_pkg = + { buildLinux, ... }@args: + let + version = "6.7.0"; + modDirVersion = "${version}"; + in + buildLinux ( + args + // { + inherit version modDirVersion; + + src = sources.jhovold-linux; + + kernelPatches = (args.kernelPatches or [ ]) ++ kp; + extraMeta.branch = lib.versions.majorMinor version; + } + // (args.argsOverride or { }) + ); +in +rec { + "x13s/linux" = pkgs.callPackage linux_x13s_pkg { defconfig = "johan_defconfig"; }; + "x13s/alsa-ucm-conf" = pkgs.alsa-ucm-conf.overrideAttrs ( + prev: rec { + version = "1.2.11-unstable-${builtins.substring 0 7 src.revision}"; + src = sources.alsa-ucm-conf; + } + ); + + pd-mapper = pkgs.callPackage ./pd-mapper { inherit qrtr; }; + qrtr = pkgs.callPackage ./qrtr { }; + + "x13s/extra-firmware" = pkgs.callPackage ./extra-firmware.nix { }; +} diff --git a/packages/extra-firmware.nix b/packages/extra-firmware.nix new file mode 100644 index 0000000..2578627 --- /dev/null +++ b/packages/extra-firmware.nix @@ -0,0 +1,21 @@ +{ stdenvNoCC, fetchFromGitHub }: + +stdenvNoCC.mkDerivation { + pname = "x13s-extra-firmware"; + version = "1.0.0"; + + src = fetchFromGitHub { + owner = "ironrobin"; + repo = "x13s-alarm"; + rev = "efa51c3b519f75b3983aef67855b1561d9828771"; + sha256 = "sha256-weETbWXz9aL2pDQDKk7fkb1ecQH0qrhUYDs2E5EiJcI="; + }; + + dontFixup = true; + dontBuild = true; + + installPhase = '' + mkdir -p $out/lib/firmware/qcom/sc8280xp/LENOVO/21BX + cp x13s-firmware/qcvss8280.mbn $out/lib/firmware/qcom/sc8280xp/LENOVO/21BX/ + ''; +} diff --git a/packages/part.nix b/packages/part.nix new file mode 100644 index 0000000..1686f58 --- /dev/null +++ b/packages/part.nix @@ -0,0 +1,6 @@ +{ lib, withSystem, ... }: +{ + flake.packages.aarch64-linux = withSystem "aarch64-linux" ( + { pkgs, ... }: import ./default.nix { inherit lib pkgs; } + ); +} diff --git a/packages/pd-mapper/default.nix b/packages/pd-mapper/default.nix new file mode 100644 index 0000000..d0f4fde --- /dev/null +++ b/packages/pd-mapper/default.nix @@ -0,0 +1,36 @@ +{ + stdenv, + lib, + fetchFromGitHub, + qrtr, + xz, +}: + +stdenv.mkDerivation { + pname = "pd-mapper"; + version = "unstable-2023-09-01"; + + buildInputs = [ + qrtr + xz + ]; + + src = fetchFromGitHub { + owner = "andersson"; + repo = "pd-mapper"; + rev = "10997ba7c43a3787a40b6b1b161408033e716374"; + hash = "sha256-qGrYNoPCxtdpTdbkSmB39+6/pSXml96Aul8b9opF9Lc="; + }; + + patches = [ ./pd-mapper-firmware-path.diff ]; + + installFlags = [ "prefix=$(out)" ]; + + meta = with lib; { + description = "Qualcomm PD mapper"; + homepage = "https://github.com/andersson/pd-mapper"; + license = licenses.bsd3; + mainProgram = "pd-mapper"; + platforms = platforms.aarch64; + }; +} diff --git a/packages/pd-mapper/pd-mapper-firmware-path.diff b/packages/pd-mapper/pd-mapper-firmware-path.diff new file mode 100644 index 0000000..696b6da --- /dev/null +++ b/packages/pd-mapper/pd-mapper-firmware-path.diff @@ -0,0 +1,13 @@ +diff --git a/pd-mapper.c b/pd-mapper.c +index 65c4b80..669d9a0 100644 +--- a/pd-mapper.c ++++ b/pd-mapper.c +@@ -194,7 +194,7 @@ static int pd_load_map(const char *file) + } + + #ifndef ANDROID +-#define FIRMWARE_BASE "/lib/firmware/" ++#define FIRMWARE_BASE "/run/current-system/firmware/" + #else + #define FIRMWARE_BASE "/vendor/firmware/" + #endif diff --git a/packages/qrtr/default.nix b/packages/qrtr/default.nix new file mode 100644 index 0000000..7dc6e44 --- /dev/null +++ b/packages/qrtr/default.nix @@ -0,0 +1,26 @@ +{ + stdenv, + lib, + fetchFromGitHub, +}: + +stdenv.mkDerivation { + pname = "qrtr"; + version = "unstable-2023-01-17"; + + src = fetchFromGitHub { + owner = "andersson"; + repo = "qrtr"; + rev = "d0d471c96e7d112fac6f48bd11f9e8ce209c04d2"; + hash = "sha256-KF0gCBRw3BDJdK1s+dYhHkokVTHwRFO58ho0IwHPehc="; + }; + + installFlags = [ "prefix=$(out)" ]; + + meta = with lib; { + description = "QMI IDL compiler"; + homepage = "https://github.com/andersson/qrtr"; + license = licenses.bsd3; + platforms = platforms.aarch64; + }; +}