From 056c577b57017fac75a0c07711419805a5a87db5 Mon Sep 17 00:00:00 2001 From: Sebastian Moser Date: Sun, 1 Sep 2024 10:56:29 +0200 Subject: [PATCH] made mizeLib in lib.nix and cross compiling support --- flake.lock | 23 ++++++++- flake.nix | 18 +++++-- lib.nix | 133 +++++++++++++++++++++++++++++++++++++++++++++++++++ webfiles.nix | 115 +++----------------------------------------- 4 files changed, 175 insertions(+), 114 deletions(-) create mode 100644 lib.nix diff --git a/flake.lock b/flake.lock index 670f18d..a83a79a 100644 --- a/flake.lock +++ b/flake.lock @@ -94,7 +94,8 @@ "fenix": "fenix", "flake-utils": "flake-utils", "mize_modules": "mize_modules", - "nixpkgs": "nixpkgs" + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay" } }, "rust-analyzer-src": { @@ -114,6 +115,26 @@ "type": "github" } }, + "rust-overlay": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1725157860, + "narHash": "sha256-DhqyM7XJYKj+WAEaYwMtXaYX66tA+lOd31sd5QkxLDM=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "1fd72e343c6890f695243a37b367a1e3b90a49ee", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + }, "systems": { "locked": { "lastModified": 1681028828, diff --git a/flake.nix b/flake.nix index ee8a534..85025f0 100644 --- a/flake.nix +++ b/flake.nix @@ -13,6 +13,11 @@ inputs.nixpkgs.follows = "nixpkgs"; }; + rust-overlay = { + url = "github:oxalica/rust-overlay"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + mize_modules = { url = "github:c2vi/mize-modules"; flake = false; @@ -20,7 +25,7 @@ flake-utils.url = "github:numtide/flake-utils"; }; - outputs = { self, flake-utils, nixpkgs, fenix, crane, mize_modules, ... }@inputs: flake-utils.lib.eachDefaultSystem (system: + outputs = { self, flake-utils, nixpkgs, fenix, crane, mize_modules, rust-overlay, ... }@inputs: flake-utils.lib.eachDefaultSystem (system: ############################## LET BINDINGS ############################## let @@ -46,6 +51,12 @@ let }; }; + mizeLib = import ./lib.nix { + inherit inputs nixpkgs self osCrane defaultMizeConfig mize_modules; + inherit rust-overlay crane; + localSystem = system; + }; + in { ############################## PACKAGES ############################## @@ -53,9 +64,8 @@ in { osCrane = osCrane; - webfiles = pkgs.callPackage ./webfiles.nix { - inherit inputs nixpkgs self osCrane defaultMizeConfig mize_modules; - localSystem = system; + webfiles = pkgs.callPackage ./webfiles.nix { + inherit (mizeLib) buildMizeForSystem mkInstallPhase; }; default = osCrane.buildPackage { diff --git a/lib.nix b/lib.nix new file mode 100644 index 0000000..bf6251c --- /dev/null +++ b/lib.nix @@ -0,0 +1,133 @@ +{ stdenv +, pkgs +, osCrane +, self +, defaultMizeConfig +, mize_modules +, localSystem +, nixpkgs +, rust-overlay +, crane +, ... +}: +rec { + # get a list of all modules, that have a mize_module.nix in their folder + module_list_drv = pkgs.stdenv.mkDerivation { + name = "mize_module_list"; + dontUnpack = true; + configurePhase = ""; + buildPhase = '' + mkdir -p $out + touch $out/modules_to_build + + for d in ${mize_modules}/modules/*; do + [[ -f $d/mize_module.nix ]] && echo "$d/mize_module.nix" >> $out/modules_to_build + done + + exit 0 + ''; + }; + module_list_string = builtins.readFile "${module_list_drv}/modules_to_build"; + module_list = pkgs.lib.lists.remove "" (pkgs.lib.strings.splitString "\n" module_list_string); + + + + toolchain_version_drv = pkgs.stdenv.mkDerivation { + name = "toolchain_version_drv"; + dontUnpack = true; + configurePhase = ""; + buildPhase = '' + mkdir -p $out + touch $out/rustc-version + + ${osCrane.rustc}/bin/rustc --version > $out/rustc-version + + exit 0 + ''; + }; + toolchain_version_string = pkgs.lib.strings.removeSuffix "\n" (builtins.readFile "${toolchain_version_drv}/rustc-version"); + + + + buildMizeForSystem = system: let + crossSystem = (if builtins.isString system then pkgs.lib.systems.mkSystemFrommString system else system) + // { + name = if crossSystem.vendor == "unknown" + then "${crossSystem.cpu.name}-${crossSystem.kernel.name}-${crossSystem.abi.name}" + else "${crossSystem.cpu.name}-${crossSystem.vendor.name}-${crossSystem.kernel.name}-${crossSystem.abi.name}"; + nameFull = "${crossSystem.cpu.name}-${crossSystem.vendor.name}-${crossSystem.kernel.name}-${crossSystem.abi.name}"; + }; + pkgsCross = import nixpkgs { inherit crossSystem localSystem; overlays = [ rust-overlay ]; }; + + craneLib = (crane.mkLib pkgsCross).overrideToolchain (p: p.rust-bin.stable.latest.default); + + mkSelString = attrs: builtins.toJSON (attrs // { + toolchain_version = toolchain_version_string; + mize_version = main.version; + }); + + mkMizeModule = attrs: { + a = builtins.error "todo!()"; + }; + + mkMizeRustModule = attrs: craneLib.buildPackage attrs // { + cargoExtraArgs = "--target ${crossSystem.nameFull} " + attrs.cargoExtraArgs or ""; + "CARGO_TARGET_${pkgsCross.lib.strings.toUpper crossSystem.nameFull}_LINKER" = "${pkgsCross.stdenv.cc.targetPrefix}cc"; + HOST_CC = "${pkgsCross.stdenv.cc.nativePrefix}cc"; + TARGET_CC = "${pkgsCross.stdenv.cc.targetPrefix}cc"; + }; + + buildModule = path: (pkgsCross.callPackage path { + inherit mkSelString craneLib; + inherit mkMizeModule mkMizeRustModule; + toolchain_version = toolchain_version_string; + mize_version = main.version; + }); + + main = craneLib.buildPackage { + src = "${self}"; + cargoExtraArgs = "--bin mize --features os-target"; + + MIZE_BUILD_CONFIG = pkgs.writeTextFile { + name = "mize-build-config"; + text = builtins.toJSON (defaultMizeConfig // { + toolchain_version = toolchain_version_string; + mize_version = main.version; + }); + }; + # patch the interpreter to run on most linux-gnu distros + postBuild = '' + patchelf --set-interpreter /lib64/ld-linux-x86-64.so.2 /build/source/target/release/mize + ''; + }; + in rec { + inherit main; + + system = crossSystem; + + modules = pkgs.lib.lists.flatten (map buildModule module_list); + }; + + + + mkInstallPhase = mize: '' + # install mize + mkdir -p $out/mize/${mize.system.name} + cp ${mize.main}/bin/mize $out/mize/${mize.system.name}/ + + # intall the modules + ${mkModulesInstallPhase mize.modules} + ''; + + mkModulesInstallPhase = modules: pkgs.lib.concatStringsSep "\n" (map (module: '' + echo got module: ${module.name} + hash=$(echo ${module.selector_string} | sha256sum | cut -c -32) + mkdir -p $out/mize/dist/$hash + cp -r ${module}/* $out/mize/dist/$hash + echo '${module.selector_string}' > $out/mize/dist/$hash/selector_string + '')) modules; + + +} + + diff --git a/webfiles.nix b/webfiles.nix index 900497d..b5050a1 100644 --- a/webfiles.nix +++ b/webfiles.nix @@ -1,119 +1,16 @@ { stdenv , pkgs -, osCrane -, self -, defaultMizeConfig -, mize_modules -, localSystem +, mkInstallPhase +, buildMizeForSystem , ... -}: -let +}: let - # get a list of all modules, that have a mize_module.nix in their folder - module_list_drv = pkgs.stdenv.mkDerivation { - name = "mize_module_list"; - dontUnpack = true; - configurePhase = ""; - buildPhase = '' - mkdir -p $out - touch $out/modules_to_build - - for d in ${mize_modules}/modules/*; do - [[ -f $d/mize_module.nix ]] && echo "$d/mize_module.nix" >> $out/modules_to_build - done - - exit 0 - ''; - }; - module_list_string = builtins.readFile "${module_list_drv}/modules_to_build"; - module_list = pkgs.lib.lists.remove "" (pkgs.lib.strings.splitString "\n" module_list_string); - - - - toolchain_version_drv = pkgs.stdenv.mkDerivation { - name = "toolchain_version_drv"; - dontUnpack = true; - configurePhase = ""; - buildPhase = '' - mkdir -p $out - touch $out/rustc-version - - ${osCrane.rustc}/bin/rustc --version > $out/rustc-version - - exit 0 - ''; - }; - toolchain_version_string = pkgs.lib.strings.removeSuffix "\n" (builtins.readFile "${toolchain_version_drv}/rustc-version"); - - - - buildMizeForSystem = system: let - crossSystem = if builtins.isString system then { config = system; } else system; - craneLib = osCrane; - - mkSelString = attrs: builtins.toJSON (attrs // { - toolchain_version = toolchain_version_string; - mize_version = main.version; - }); - - buildModule = path: (pkgs.callPackage path { - inherit mkSelString; - craneLib = osCrane; - toolchain_version = toolchain_version_string; - }); - - main = craneLib.buildPackage { - src = "${self}"; - cargoExtraArgs = "--bin mize --features os-target"; - - MIZE_BUILD_CONFIG = pkgs.writeTextFile { - name = "mize-build-config"; - text = builtins.toJSON (defaultMizeConfig // { - toolchain_version = toolchain_version_string; - mize_version = main.version; - }); - }; - # patch the interpreter to run on most linux-gnu distros - postBuild = '' - patchelf --set-interpreter /lib64/ld-linux-x86-64.so.2 /build/source/target/release/mize - ''; - }; - in rec { - inherit main; - - system = crossSystem; - - modules = map buildModule module_list; - }; - - - - mkInstallPhase = mize: '' - # install mize - mkdir -p $out/mize/${mize.system.config} - cp ${mize.main}/bin/mize $out/mize/${mize.system.config}/ - - # intall the modules - ${mkModulesInstallPhase mize.modules} - ''; - - mkModulesInstallPhase = modules: pkgs.lib.concatStringsSep "\n" (map (module: '' - echo got module: ${module.name} - hash=$(echo ${module.selector_string} | sha256sum | cut -c -32) - mkdir -p $out/mize/dist/$hash - cp -r ${module}/* $out/mize/dist/$hash - echo '${module.selector_string}' > $out/mize/dist/$hash/selector_string - '')) modules; - - - - # list of systems, we build mize for - systems = [ "x86_64-linux-gnu" ]; + # list of systems, we build mize for for webfiles + systems = [ "x86_64-linux" "aarch64-linux" "wasm32" ]; list_of_mizes = map buildMizeForSystem systems; -in - +in stdenv.mkDerivation { name = "mize-webfiles"; dontUnpack = true;