diff --git a/.gitignore b/.gitignore index b38b454..8c4febe 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ bin *.exp *.pdb bake +result diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..07d5f28 --- /dev/null +++ b/flake.lock @@ -0,0 +1,61 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1762596750, + "narHash": "sha256-rXXuz51Bq7DHBlfIjN7jO8Bu3du5TV+3DSADBX7/9YQ=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "b6a8526db03f735b89dd5ff348f53f752e7ddc8e", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..d0dc2b6 --- /dev/null +++ b/flake.nix @@ -0,0 +1,144 @@ +{ + description = "Bake - A build system and package manager for C/C++ projects"; + + # Key challenges solved in this flake: + # 1. Bake requires building not just the main binary, but also language drivers + # 2. Drivers must be built using bake itself (via `bake setup`) + # 3. Bake needs a writable BAKE_HOME for project metadata, conflicting with + # Nix's read-only store + # 4. Solution: Initialize a writable ~/bake on first run, copied from the Nix store + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = { self, nixpkgs, flake-utils }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = nixpkgs.legacyPackages.${system}; + + # Map Nix system to bake build directory + buildDirMap = { + "x86_64-linux" = "build-Linux"; + "aarch64-linux" = "build-Linux"; + "x86_64-darwin" = "build-Darwin"; + "aarch64-darwin" = "build-Darwin"; + }; + + buildDir = buildDirMap.${system} or (throw "Unsupported system: ${system}"); + + in { + packages = { + default = pkgs.stdenv.mkDerivation rec { + pname = "bake"; + version = "2.5.0"; + + src = ./.; + + nativeBuildInputs = [ pkgs.gnumake pkgs.makeWrapper ]; + + buildPhase = '' + runHook preBuild + + # Build main bake binary first using make + make -C ${buildDir} clean all + + # Use bake itself to build language drivers and utilities + # This is the proper way to build bake - drivers can't be built with + # make alone due to complex include path dependencies + + # Set HOME to a temporary directory so bake installs to $HOME/bake + export HOME=$PWD/fake-home + mkdir -p $HOME + + # Run bake setup with --local to: + # - Build bake.util (utility library) + # - Build bake.lang.c and bake.lang.cpp (language drivers) + # - Skip /usr/local/bin installation (--local flag) + ./bake setup --local + + runHook postBuild + ''; + + installPhase = '' + runHook preInstall + + mkdir -p $out/bin + mkdir -p $out/share/bake + + # Copy the entire bake environment that was created by bake setup + # This includes: lib/, bin/, include/, meta/, templates/, etc. + # Use -L to dereference symlinks - bake setup creates symlinks back to + # the source directory, which would be dangling in the Nix store + cp -rL fake-home/bake/* $out/share/bake/ + + # Ensure the bake binary is present + if [ ! -f $out/share/bake/bin/bake ]; then + mkdir -p $out/share/bake/bin + cp bake $out/share/bake/bin/bake + fi + + # Create a wrapper script that handles the read-only Nix store issue + # Problem: Bake needs BAKE_HOME to be writable (for project metadata), + # but Nix store is read-only + # Solution: On first run, copy the Nix store bake environment to ~/bake + # and use that as BAKE_HOME + cat > $out/bin/bake <