From dd07e99ef645dd8c02e97e294d2a9b33b8716c20 Mon Sep 17 00:00:00 2001 From: Emery Hemingway Date: Wed, 20 Nov 2019 18:48:56 +0100 Subject: [PATCH] Nix flake --- .gitignore | 15 +++++ configs/.gitignore | 0 default.nix | 160 +++++++++++++++++++++++++++++++++++++++++++++ flake.lock | 11 ++++ flake.nix | 21 ++++++ setup-hooks.sh | 7 ++ shell.nix | 2 +- 7 files changed, 215 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 configs/.gitignore create mode 100644 default.nix create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 setup-hooks.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..c7f922bb1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +*~ +*.orig +*.swp +*.rej +result +result-* + +/build +/contrib +/depot +/public +/repos/world +/.tup +/build-* +/configs diff --git a/configs/.gitignore b/configs/.gitignore new file mode 100644 index 000000000..e69de29bb diff --git a/default.nix b/default.nix new file mode 100644 index 000000000..39a8ac4ee --- /dev/null +++ b/default.nix @@ -0,0 +1,160 @@ +{ system ? "x86_64-genode", self ? { }, nixpkgs ? import { + localSystem = "x86_64-linux"; + crossSystem = "x86_64-genode"; +} }: +let + inherit (nixpkgs) stdenv buildPackages fetchgit llvmPackages; + + src = self.outPath or ./.; + version = self.lastModified or "unstable"; + + inherit (stdenv) lib targetPlatform; + specs = with targetPlatform; + [ ] ++ lib.optional is32bit "32bit" ++ lib.optional is64bit "64bit" + ++ lib.optional isAarch32 "arm" ++ lib.optional isAarch64 "arm_64" + ++ lib.optional isRiscV "riscv" ++ lib.optional isx86 "x86" + ++ lib.optional isx86_32 "x86_32" ++ lib.optional isx86_64 "x86_64"; + + buildRepo = { repo, repoInputs }: + let + tupArch = with stdenv.targetPlatform; + if isAarch32 then + "arm" + else if isAarch64 then + "arm64" + else if isx86_32 then + "i386" + else if isx86_64 then + "x86_64" + else + abort "unhandled targetPlatform"; + + toTupConfig = attrs: + with builtins; + let op = config: name: "${config}CONFIG_${name}=${getAttr name attrs} "; + in foldl' op "" (attrNames attrs); + + in stdenv.mkDerivation { + name = "genode-${repo}-${version}"; + outputs = [ "out" "dev" ]; + inherit src repo specs version; + + setupHook = ./setup-hooks.sh; + + nativeBuildInputs = repoInputs; + # This is wrong, why does pkg-config not collect buildInputs? + + propagatedNativeBuildInputs = repoInputs; + + depsBuildBuild = with buildPackages; [ llvm pkgconfig tup ]; + + tupConfig = toTupConfig { + LIBCXX = llvmPackages.libcxx; + LIBCXXABI = llvmPackages.libcxxabi; + LIBUNWIND = llvmPackages.libunwind; + LIBUNWIND_BAREMETAL = + llvmPackages.libunwind.override { isBaremetal = true; }; + LINUX_HEADERS = buildPackages.glibc.dev; + OLEVEL = "-O2"; + TUP_ARCH = tupArch; + VERSION = version; + }; + + configurePhase = '' + # Configure Tup + echo $tupConfig | tr ' CONFIG_' '\nCONFIG_' > tup.config + echo CONFIG_NIX_OUTPUTS_OUT=$out >> tup.config + echo CONFIG_NIX_OUTPUTS_DEV=$dev >> tup.config + + # Disable other repos + for R in repos/*; do + [ "$R" != "repos/$repo" ] && find $R -name Tupfile -delete + done + find repos/gems -name Tupfile -delete + + # Scan repository and generate script + tup init + tup generate buildPhase.sh + + # Redirect artifacts to Nix store + mkdir -p $out/lib $dev/include + ln -s $out out + ln -s $dev dev + ''; + + buildPhase = '' + test -d repos/$repo/src/ld && cp -rv repos/$repo/src/ld $dev/ + pushd . + set -v + source buildPhase.sh + set +v + popd + ''; + + installPhase = '' + # Populate the "dev" headers + if [ -d "repos/$repo/include" ]; then + for DIR in repos/$repo/include; do + for SPEC in $specs; do + if [ -d $DIR/spec/$SPEC ]; then + cp -r $DIR/spec/$SPEC/* $dev/include + rm -r $DIR/spec/$SPEC + fi + done + rm -rf $DIR/spec + cp -r $DIR $dev/ + done + fi + + touch $dev/.genode + for pc in $dev/lib/pkgconfig/*.pc; do + sed -e "s|^Libs: |Libs: -L$dev/lib |" -i $pc + done + ''; + + meta = with stdenv.lib; { + description = + "The Genode operation system framework (${repo} repository)."; + homepage = "https://genode.org/"; + license = licenses.agpl3; + maintainers = [ maintainers.ehmry ]; + }; + + shellHook = '' + export PROMPT_DIRTRIM=2 + export PS1="\[\033[1;30m\]Genode-dev [\[\033[1;37m\]\w\[\033[1;30m\]] $\[\033[0m\] " + export PS2="\[\033[1;30m\]>\[\033[0m\] " + if [ -e "configs/.gitignore" ]; then + local CFG=configs/${targetPlatform.config}.config + echo $tupConfig | tr ' CONFIG_' '\nCONFIG_' > $CFG + tup variant $CFG + fi + ''; + }; + +in rec { + base = buildRepo { + repo = "base"; + repoInputs = [ ]; + }; + + base-linux = buildRepo { + repo = "base-linux"; + repoInputs = [ base ]; + }; + + base-nova = buildRepo { + repo = "base-nova"; + repoInputs = [ base ]; + }; + + os = buildRepo { + repo = "os"; + repoInputs = [ base ]; + }; + + gems = buildRepo { + repo = "gems"; + repoInputs = [ os ]; + }; +} diff --git a/flake.lock b/flake.lock new file mode 100644 index 000000000..073a86267 --- /dev/null +++ b/flake.lock @@ -0,0 +1,11 @@ +{ + "inputs": { + "nixpkgs": { + "inputs": {}, + "narHash": "sha256-HYX9rCn1dweN+L3ecNy4BiCGw4WEt1zgdIiAeYNY5GM=", + "originalUrl": "git+https://gitea.c3d2.de/ehmry/nixpkgs.git?ref=genode", + "url": "git+https://gitea.c3d2.de/ehmry/nixpkgs.git?ref=genode&rev=12e31ac07c6d337760b3f59dc64c03f26cdd2009" + } + }, + "version": 3 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 000000000..8d0329ae0 --- /dev/null +++ b/flake.nix @@ -0,0 +1,21 @@ +{ + edition = 201909; + + description = "Genode system flake"; + + inputs.nixpkgs.uri = "git+https://gitea.c3d2.de/ehmry/nixpkgs.git?ref=genode"; + + outputs = { self, nixpkgs }: + let + systems = [ "x86_64-genode" ]; + forAllSystems = f: nixpkgs.lib.genAttrs systems (system: f system); + in { + packages = forAllSystems (system: + import ./default.nix { + inherit system self; + nixpkgs = builtins.getAttr system nixpkgs.legacyPackages; + }); + + defaultPackage = forAllSystems (system: self.packages."${system}".os); + }; +} diff --git a/setup-hooks.sh b/setup-hooks.sh new file mode 100644 index 000000000..b4d96c5de --- /dev/null +++ b/setup-hooks.sh @@ -0,0 +1,7 @@ +addGenodePkgConfigPath () { + if test -e "$1/.genode" && test -d "$1/lib/pkgconfig"; then + export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:$1/lib/pkgconfig" + fi +} + +addEnvHooks "$targetOffset" addGenodePkgConfigPath diff --git a/shell.nix b/shell.nix index 4436f3b1c..77586954e 100644 --- a/shell.nix +++ b/shell.nix @@ -1 +1 @@ -{ pkgs ? import { } }: pkgs.genode.base +let pkgs = import ./default.nix { }; in pkgs.base