From 834d7d56c9e6973d092729e1d7af40f3f28607ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Baylac=20Jacqu=C3=A9?= Date: Tue, 22 Nov 2022 10:23:20 +0100 Subject: [PATCH] Add Nix build infrastructure --- default.nix | 30 +++++++++++++++++++++++ flake.lock | 27 +++++++++++++++++++++ flake.nix | 28 ++++++++++++++++++++++ nixglhost-wrapper.py | 57 ++++++++++++++++++++++++++++++++++++++++++++ shell.nix | 9 +++++++ 5 files changed, 151 insertions(+) create mode 100644 default.nix create mode 100644 flake.lock create mode 100644 flake.nix create mode 100755 nixglhost-wrapper.py create mode 100644 shell.nix diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..29fe6cb --- /dev/null +++ b/default.nix @@ -0,0 +1,30 @@ +{ pkgs ? import { }, lib ? pkgs.lib }: + +pkgs.stdenvNoCC.mkDerivation { + pname = "nix-gl-host"; + version = "0.1"; + # TODO: filter that out + src = lib.cleanSource ./.; + nativeBuildInputs = [ + pkgs.nixpkgs-fmt + pkgs.python3 + pkgs.python3Packages.black + pkgs.nixpkgs-fmt + ]; + + postFixup = '' + substituteInPlace $out/bin/nixglhost \ + --replace "@patchelf-bin@" "${pkgs.patchelf}/bin/patchelf" \ + --replace "IN_NIX_STORE = False" "IN_NIX_STORE = True" + patchShebangs $out/bin/nixglhost + ''; + + postCheck = '' + black --check $out/bin/nixglhost + nixpkgs-fmt --check *.nix + ''; + + installPhase = '' + install -D -m0755 nixglhost-wrapper.py $out/bin/nixglhost + ''; +} diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..5887140 --- /dev/null +++ b/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1669045945, + "narHash": "sha256-BQxzijvZpLQ7R+KuQzCPcFgIS6OK0Onb29pYFe2pzJo=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "6b5019a48f876f3288efc626fa8b70ad0c64eb46", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..065de7d --- /dev/null +++ b/flake.nix @@ -0,0 +1,28 @@ +{ + description = "Gluing native OpenGL drivers"; + inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + outputs = + { self + , nixpkgs + }: + let + systems = [ + "x86_64-linux" + "aarch64-linux" + ]; + forAllSystems = f: nixpkgs.lib.genAttrs systems (system: f system); + in + { + defaultPackage = forAllSystems (system: + import ./default.nix { + pkgs = import nixpkgs { inherit system; }; + }); + devShell = forAllSystems (system: + nixpkgs.legacyPackages.${system}.callPackage ./shell.nix { } + ); + + formatter = forAllSystems (system: + nixpkgs.legacyPackages.${system}.nixpkgs-fmt + ); + }; +} diff --git a/nixglhost-wrapper.py b/nixglhost-wrapper.py new file mode 100755 index 0000000..eb14b08 --- /dev/null +++ b/nixglhost-wrapper.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 +import argparse +import os +import sys + +IN_NIX_STORE = False + +if IN_NIX_STORE: + # The following paths are meant to be substituted by Nix at build + # time. + PATCHELF_PATH = "@patchelf-bin@" +else: + PATCHELF_PATH = "patchelf" + + +def info_debug(string): + """Prints STR to STDERR if the DEBUG environment variable is set""" + if "DEBUG" in os.environ: + print(f"[+] {string}", file=sys.stderr) + + +def patch_dso(dsoPath, ): + raise "TODO patch_dso" + + +def find_vendor_dso(): + raise "TODO find_vendor_dso" + + +def exec_binary(args): + raise "TODO exec_binary" + + +def main(args): + home = os.path.expanduser("~") + xdg_cache_home = os.environ.get("XDG_CACHE_HOME", os.path.join(HOME, ".cache")) + os.exit(0) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + prog="nixglhost-wrapper", + description="Wrapper used to massage the host GL drivers to work with your nix-built binary.", + ) + parser.add_argument( + "GL_VENDOR_PATH", + type=str, + help="a path pointing to the directory containing your GL driver shared libraries", + ) + parser.add_argument( + "NIX_BINARY_AND_ARGS", + type=str, + nargs="+", + help="Nix-built binary you'd like to wrap and its args. For instance: nixglhost-wrapper /usr/lib/nvidia opengl-exe --with --some --args", + ) + args = parser.parse_args() + main() diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..7d88dab --- /dev/null +++ b/shell.nix @@ -0,0 +1,9 @@ +{ pkgs ? import { } }: + +pkgs.mkShell { + nativeBuildInputs = [ + pkgs.nixpkgs-fmt + pkgs.python3Packages.black + pkgs.python3Packages.mypy + ]; +}