From c4360ec98aa5d62456d08d471926e1d999071163 Mon Sep 17 00:00:00 2001 From: Christoph Urlacher Date: Mon, 30 Jun 2025 22:46:27 +0200 Subject: [PATCH] Update env --- .envrc | 1 - .gitignore | 3 +- flake.lock | 110 ++++++++---------- flake.nix | 324 +++++++++++++++++++++++++++++++++++++---------------- 4 files changed, 277 insertions(+), 161 deletions(-) delete mode 100644 .envrc diff --git a/.envrc b/.envrc deleted file mode 100644 index 3550a30..0000000 --- a/.envrc +++ /dev/null @@ -1 +0,0 @@ -use flake diff --git a/.gitignore b/.gitignore index 66fed7f..0d4714f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .direnv -cmake-build-debug \ No newline at end of file +cmake-build-debug +result diff --git a/flake.lock b/flake.lock index 559fe29..b63c567 100644 --- a/flake.lock +++ b/flake.lock @@ -1,46 +1,15 @@ { "nodes": { - "devshell": { - "inputs": { - "flake-utils": "flake-utils", - "nixpkgs": "nixpkgs" - }, - "locked": { - "lastModified": 1678957337, - "narHash": "sha256-Gw4nVbuKRdTwPngeOZQOzH/IFowmz4LryMPDiJN/ah4=", - "owner": "numtide", - "repo": "devshell", - "rev": "3e0e60ab37cd0bf7ab59888f5c32499d851edb47", - "type": "github" - }, - "original": { - "owner": "numtide", - "repo": "devshell", - "type": "github" - } - }, "flake-utils": { - "locked": { - "lastModified": 1642700792, - "narHash": "sha256-XqHrk7hFb+zBvRg6Ghl+AZDq03ov6OshJLiSWOoX5es=", - "owner": "numtide", - "repo": "flake-utils", - "rev": "846b2ae0fc4cc943637d3d1def4454213e203cba", - "type": "github" + "inputs": { + "systems": "systems" }, - "original": { - "owner": "numtide", - "repo": "flake-utils", - "type": "github" - } - }, - "flake-utils_2": { "locked": { - "lastModified": 1678901627, - "narHash": "sha256-U02riOqrKKzwjsxc/400XnElV+UtPUQWpANPlyazjH0=", + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", "owner": "numtide", "repo": "flake-utils", - "rev": "93a2b84fc4b70d9e089d029deacc3583435c2ed6", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", "type": "github" }, "original": { @@ -51,41 +20,58 @@ }, "nixpkgs": { "locked": { - "lastModified": 1677383253, - "narHash": "sha256-UfpzWfSxkfXHnb4boXZNaKsAcUrZT9Hw+tao1oZxd08=", + "lastModified": 1751180975, + "narHash": "sha256-BKk4yDiXr4LdF80OTVqYJ53Q74rOcA/82EClXug8xsY=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "9952d6bc395f5841262b006fbace8dd7e143b634", + "rev": "a48741b083d4f36dd79abd9f760c84da6b4dc0e5", "type": "github" }, "original": { - "owner": "NixOS", - "ref": "nixpkgs-unstable", - "repo": "nixpkgs", - "type": "github" - } - }, - "nixpkgs_2": { - "locked": { - "lastModified": 1679281263, - "narHash": "sha256-neMref1GTruSLt1jBgAw+lvGsZj8arQYfdxvSi5yp4Q=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "8276a165b9fa3db1a7a4f29ee29b680e0799b9dc", - "type": "github" - }, - "original": { - "owner": "NixOS", - "ref": "nixpkgs-unstable", - "repo": "nixpkgs", - "type": "github" + "id": "nixpkgs", + "type": "indirect" } }, "root": { "inputs": { - "devshell": "devshell", - "flake-utils": "flake-utils_2", - "nixpkgs": "nixpkgs_2" + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay" + } + }, + "rust-overlay": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1751251399, + "narHash": "sha256-y+viCuy/eKKpkX1K2gDvXIJI/yzvy6zA3HObapz9XZ0=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "b22d5ee8c60ed1291521f2dde48784edd6bf695b", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" } } }, diff --git a/flake.nix b/flake.nix index 883c422..170a943 100644 --- a/flake.nix +++ b/flake.nix @@ -1,117 +1,247 @@ -{ - description = "Logisim Assembler Development Environment"; +rec { + description = "Logisim Assembler"; - inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; - inputs.flake-utils.url = "github:numtide/flake-utils"; - inputs.devshell.url = "github:numtide/devshell"; + inputs = { + nixpkgs.url = "nixpkgs"; # Use nixpkgs from system registry + flake-utils.url = "github:numtide/flake-utils"; + + rust-overlay.url = "github:oxalica/rust-overlay"; + rust-overlay.inputs.nixpkgs.follows = "nixpkgs"; + }; outputs = { self, nixpkgs, flake-utils, - devshell, + rust-overlay, }: + # Create a shell (and possibly package) for each possible system, not only x86_64-linux flake-utils.lib.eachDefaultSystem (system: let pkgs = import nixpkgs { inherit system; - config.allowUnfree = true; # For clion - overlays = [devshell.overlays.default]; - }; - - # NOTE: Usual 64 bit compilers that don't collide - bintools = pkgs.wrapBintoolsWith { - bintools = pkgs.bintools.bintools; - libc = pkgs.glibc; - }; - gcc12 = pkgs.hiPrio (pkgs.wrapCCWith { - cc = pkgs.gcc12.cc; - libc = pkgs.glibc; - bintools = bintools; - }); - clang15 = pkgs.wrapCCWith { - cc = pkgs.clang_15.cc; - libc = pkgs.glibc; - bintools = bintools; - }; - - # NOTE: Multilib compilers that don't collide - bintools_multi = pkgs.wrapBintoolsWith { - bintools = pkgs.bintools.bintools; # Get the unwrapped bintools from the wrapper - libc = pkgs.glibc_multi; - }; - gcc12_multi = pkgs.hiPrio (pkgs.wrapCCWith { - cc = pkgs.gcc12.cc; # Get the unwrapped gcc from the wrapper - libc = pkgs.glibc_multi; - bintools = bintools_multi; - }); - clang15_multi = pkgs.wrapCCWith { - cc = pkgs.clang_15.cc; - libc = pkgs.glibc_multi; - bintools = bintools_multi; - }; - in { - devShells.default = pkgs.mkShell { - buildInputs = with pkgs; [ - # Compilers - bintools - gcc12 - clang15 - # bintools_multi - # gcc12_multi - # clang14_multi - - # Libraries - boost181 - - # Native buildinputs - gnumake - cmake - # nasm - - # Development - # bear # To generate compilation database - gdb - cling # To try out my bullshit implementations - # doxygen # Generate docs + graphs + config.allowUnfree = true; + overlays = [ + rust-overlay.overlays.default ]; }; + inherit (pkgs) lib stdenv; - # TODO: DevShell doesn't propagate buildinputs, so its difficult to find e.g. Boost... - # # devShell = pkgs.devshell.mkShell ... - # devShell = pkgs.devshell.mkShell { - # name = "Logisim Assembler Development Environment"; + # =========================================================================================== + # Define custom dependencies + # =========================================================================================== - # packages = with pkgs; [ - # # Compilers - # bintools - # gcc12 - # clang15 - # # bintools_multi - # # gcc12_multi - # # clang14_multi + python = pkgs.python313.withPackages (p: + with p; [ + # numpy + # matplotlib + # ffmpeg-python + # pyside6 + ]); - # # Libraries - # boost181 + rust = pkgs.rust-bin.stable.latest.default.override { + extensions = ["rust-src"]; # Include the Rust stdlib source (for IntelliJ) + }; - # # Native buildinputs - # gnumake - # cmake - # # nasm + # 64 bit C/C++ compilers that don't collide (use the same libc) + bintools = pkgs.wrapBintoolsWith { + bintools = pkgs.bintools.bintools; # Unwrapped bintools + libc = pkgs.glibc; + }; + gcc = pkgs.hiPrio (pkgs.wrapCCWith { + cc = pkgs.gcc.cc; # Unwrapped gcc + libc = pkgs.glibc; + bintools = bintools; + }); + clang = pkgs.wrapCCWith { + cc = pkgs.clang.cc; # Unwrapped clang + libc = pkgs.glibc; + bintools = bintools; + }; - # # Development - # # bear # To generate compilation database - # gdb - # cling # To try out my bullshit implementations - # # doxygen # Generate docs + graphs - # ]; + # Multilib C/C++ compilers that don't collide (use the same libc) + bintools_multilib = pkgs.wrapBintoolsWith { + bintools = pkgs.bintools.bintools; # Unwrapped bintools + libc = pkgs.glibc_multi; + }; + gcc_multilib = pkgs.hiPrio (pkgs.wrapCCWith { + cc = pkgs.gcc.cc; # Unwrapped gcc + libc = pkgs.glibc_multi; + bintools = bintools_multilib; + }); + clang_multilib = pkgs.wrapCCWith { + cc = pkgs.clang.cc; # Unwrapped clang + libc = pkgs.glibc_multi; + bintools = bintools_multilib; + }; - # commands = [ - # { - # name = "ide"; - # help = "Run clion for project"; - # command = "clion &>/dev/null ./ &"; - # } - # ]; - # }; + # =========================================================================================== + # Specify dependencies + # https://nixos.org/manual/nixpkgs/stable/#ssec-stdenv-dependencies-overview + # Just for a "nix develop" shell, buildInputs can be used for everything. + # =========================================================================================== + + # Add dependencies to nativeBuildInputs if they are executed during the build: + # - Those which are needed on $PATH during the build, for example cmake and pkg-config + # - Setup hooks, for example makeWrapper + # - Interpreters needed by patchShebangs for build scripts (with the --build flag), which can be the case for e.g. perl + nativeBuildInputs = with pkgs; [ + # Languages: + # python + # rust + bintools + gcc + clang + # bintools_multilib + # gcc_multilib + # clang_multilib + + # C/C++: + gdb + valgrind + gnumake + cmake + # pkg-config + + # Qt: + # qt6.wrapQtAppsHook # For the shellHook + ]; + + # Add dependencies to buildInputs if they will end up copied or linked into the final output or otherwise used at runtime: + # - Libraries used by compilers, for example zlib + # - Interpreters needed by patchShebangs for scripts which are installed, which can be the case for e.g. perl + buildInputs = with pkgs; [ + # C/C++: + boost + # sfml + + # Qt: + # qt6.qtbase + # qt6.full + ]; + + # =========================================================================================== + # Define buildable + installable packages + # =========================================================================================== + + package = stdenv.mkDerivation { + inherit nativeBuildInputs buildInputs; + pname = "lasm"; + version = "1.0.0"; + src = ./.; + + installPhase = '' + mkdir -p $out/bin + mv ./lasm $out/bin + ''; + }; + in rec { + # Provide package for "nix build" + defaultPackage = package; + defaultApp = flake-utils.lib.mkApp { + drv = defaultPackage; + }; + + # Provide environment for "nix develop" + devShell = pkgs.mkShell { + inherit nativeBuildInputs buildInputs; + name = description; + + # ========================================================================================= + # Define environment variables + # ========================================================================================= + + # Rust stdlib source: + # RUST_SRC_PATH = "${rust}/lib/rustlib/src/rust/library"; + + # Custom dynamic libraries: + # LD_LIBRARY_PATH = builtins.concatStringsSep ":" [ + # # Rust Bevy GUI app + # "${pkgs.xorg.libX11}/lib" + # "${pkgs.xorg.libXcursor}/lib" + # "${pkgs.xorg.libXrandr}/lib" + # "${pkgs.xorg.libXi}/lib" + # "${pkgs.libGL}/lib" + # ]; + + # Dynamic libraries from buildinputs: + # LD_LIBRARY_PATH = nixpkgs.lib.makeLibraryPath buildInputs; + + # ========================================================================================= + # Define shell environment + # ========================================================================================= + + # Setup the shell when entering the "nix develop" environment (bash script). + shellHook = let + mkCmakeScript = type: let + typeLower = lib.toLower type; + in + pkgs.writers.writeFish "cmake-${typeLower}.fish" '' + cd $FLAKE_PROJECT_ROOT + + echo "Removing build directory ./cmake-build-${typeLower}/" + rm -rf ./cmake-build-${typeLower} + + echo "Creating build directory" + mkdir cmake-build-${typeLower} + cd cmake-build-${typeLower} + + echo "Running cmake" + cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE="${type}" -DCMAKE_EXPORT_COMPILE_COMMANDS="On" .. + + echo "Linking compile_commands.json" + cd .. + ln -sf ./cmake-build-${typeLower}/compile_commands.json ./compile_commands.json + ''; + + cmakeDebug = mkCmakeScript "Debug"; + cmakeRelease = mkCmakeScript "Release"; + + mkBuildScript = type: let + typeLower = lib.toLower type; + in + pkgs.writers.writeFish "cmake-build.fish" '' + cd $FLAKE_PROJECT_ROOT/cmake-build-${typeLower} + + echo "Running cmake" + cmake --build . + ''; + + buildDebug = mkBuildScript "Debug"; + buildRelease = mkBuildScript "Release"; + + # Use this to specify commands that should be ran after entering fish shell + initProjectShell = pkgs.writers.writeFish "init-shell.fish" '' + echo "Entering \"${description}\" environment..." + + # Determine the project root, used e.g. in cmake scripts + set -g -x FLAKE_PROJECT_ROOT (git rev-parse --show-toplevel) + + # Build the provided NixOS package + abbr -a build "nix build -L" + + # Rust Bevy: + # abbr -a build-release-windows "CARGO_FEATURE_PURE=1 cargo xwin build --release --target x86_64-pc-windows-msvc" + + # C/C++: + abbr -a cmake-debug "${cmakeDebug}" + abbr -a cmake-release "${cmakeRelease}" + abbr -a build-debug "${buildDebug}" + abbr -a build-release "${buildRelease}" + ''; + in + builtins.concatStringsSep "\n" [ + # Launch into pure fish shell + '' + exec "$(type -p fish)" -C "source ${initProjectShell} && abbr -a menu '${pkgs.bat}/bin/bat "${initProjectShell}"'" + '' + + # Qt: Launch into wrapped fish shell + # '' + # fishdir=$(mktemp -d) + # makeWrapper "$(type -p fish)" "$fishdir/fish" "''${qtWrapperArgs[@]}" + # exec "$fishdir/fish" -C "source ${initProjectShell} && abbr -a menu '${pkgs.bat}/bin/bat "${initProjectShell}"'" + # '' + ]; + }; }); }