From 1ee3ea25e98cd0ef73939edd97a1e42357dcb298 Mon Sep 17 00:00:00 2001 From: Patrick Cleavelin Date: Tue, 5 Aug 2025 13:43:19 -0500 Subject: [PATCH] move complete build to nix --- .gitignore | 1 + Makefile | 10 +-- flake.lock | 58 +++++--------- flake.nix | 152 ++++++++++++++++++++++-------------- readme.md | 10 +++ src/main.odin | 2 +- src/panels/file_buffer.odin | 4 +- src/panels/grep.odin | 2 +- src/pkg/grep_lib/Cargo.toml | 2 +- src/tree_sitter/ts.odin | 10 +-- 10 files changed, 141 insertions(+), 110 deletions(-) create mode 100644 readme.md diff --git a/.gitignore b/.gitignore index 3171167..c8296f4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +result/ bin/ **/target **/*.xcodeproj diff --git a/Makefile b/Makefile index dacbd56..98e3f13 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,6 @@ -export RUSTFLAGS=-C target-feature=-avx2 +all: editor -all: bin/libtree-sitter.a editor - -editor: grep src/**/*.odin +editor: src/**/*.odin mkdir -p bin odin build src/ -out:bin/editor -debug @@ -24,5 +22,5 @@ override CFLAGS += -D_POSIX_C_SOURCE=200112L -D_DEFAULT_SOURCE override CFLAGS += -I$(TS_DIR)/src -I$(TS_DIR)/src/wasm -I$(TS_DIR)/include override CFLAGS += -o bin/ -bin/libtree-sitter.a: $(TS_OBJ) - $(AR) $(TS_ARFLAGS) $@ $^ --output bin/ +libtree-sitter.a: $(TS_OBJ) + $(AR) $(TS_ARFLAGS) $@ $^ diff --git a/flake.lock b/flake.lock index 7dbe085..d0c5792 100644 --- a/flake.lock +++ b/flake.lock @@ -5,11 +5,11 @@ "systems": "systems" }, "locked": { - "lastModified": 1705309234, - "narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=", + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", "owner": "numtide", "repo": "flake-utils", - "rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", "type": "github" }, "original": { @@ -19,30 +19,15 @@ } }, "flake-utils_2": { - "locked": { - "lastModified": 1659877975, - "narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=", - "owner": "numtide", - "repo": "flake-utils", - "rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0", - "type": "github" - }, - "original": { - "owner": "numtide", - "repo": "flake-utils", - "type": "github" - } - }, - "flake-utils_3": { "inputs": { "systems": "systems_2" }, "locked": { - "lastModified": 1681202837, - "narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=", + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", "owner": "numtide", "repo": "flake-utils", - "rev": "cfacdce06f30d2b68473a46042957675eebb3401", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", "type": "github" }, "original": { @@ -57,11 +42,11 @@ "nixpkgs": "nixpkgs" }, "locked": { - "lastModified": 1685908677, - "narHash": "sha256-E4zUPEUFyVWjVm45zICaHRpfGepfkE9Z2OECV9HXfA4=", + "lastModified": 1752054764, + "narHash": "sha256-Ob/HuUhANoDs+nvYqyTKrkcPXf4ZgXoqMTQoCK0RFgQ=", "owner": "guibou", "repo": "nixGL", - "rev": "489d6b095ab9d289fe11af0219a9ff00fe87c7c5", + "rev": "a8e1ce7d49a149ed70df676785b07f63288f53c5", "type": "github" }, "original": { @@ -72,11 +57,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1660551188, - "narHash": "sha256-a1LARMMYQ8DPx1BgoI/UN4bXe12hhZkCNqdxNi6uS0g=", + "lastModified": 1746378225, + "narHash": "sha256-OeRSuL8PUjIfL3Q0fTbNJD/fmv1R+K2JAOqWJd3Oceg=", "owner": "nixos", "repo": "nixpkgs", - "rev": "441dc5d512153039f19ef198e662e4f3dbb9fd65", + "rev": "93e8cdce7afc64297cfec447c311470788131cd9", "type": "github" }, "original": { @@ -87,11 +72,11 @@ }, "nixpkgs_2": { "locked": { - "lastModified": 1705856552, - "narHash": "sha256-JXfnuEf5Yd6bhMs/uvM67/joxYKoysyE3M2k6T3eWbg=", + "lastModified": 1754214453, + "narHash": "sha256-Q/I2xJn/j1wpkGhWkQnm20nShYnG7TI99foDBpXm1SY=", "owner": "nixos", "repo": "nixpkgs", - "rev": "612f97239e2cc474c13c9dafa0df378058c5ad8d", + "rev": "5b09dc45f24cf32316283e62aec81ffee3c3e376", "type": "github" }, "original": { @@ -103,11 +88,11 @@ }, "nixpkgs_3": { "locked": { - "lastModified": 1681358109, - "narHash": "sha256-eKyxW4OohHQx9Urxi7TQlFBTDWII+F+x2hklDOQPB50=", + "lastModified": 1744536153, + "narHash": "sha256-awS2zRgF4uTwrOKwwiJcByDzDOdo3Q1rPZbiHQg/N38=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "96ba1c52e54e74c3197f4d43026b3f3d92e83ff9", + "rev": "18dd725c29603f582cf1900e0d25f9f1063dbf11", "type": "github" }, "original": { @@ -127,15 +112,14 @@ }, "rust-overlay": { "inputs": { - "flake-utils": "flake-utils_3", "nixpkgs": "nixpkgs_3" }, "locked": { - "lastModified": 1706235145, - "narHash": "sha256-3jh5nahTlcsX6QFcMPqxtLn9p9CgT9RSce5GLqjcpi4=", + "lastModified": 1754362243, + "narHash": "sha256-QHNTUdI6oIYuuazGuKGhVk5RCOM1nIzDUc/AGgL7Szw=", "owner": "oxalica", "repo": "rust-overlay", - "rev": "3a57c4e29cb2beb777b2e6ae7309a680585b8b2f", + "rev": "3ec3244ffb877f1b7f5d2dbff19241982ab25ff2", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index 1283e28..8a1c479 100755 --- a/flake.nix +++ b/flake.nix @@ -13,80 +13,91 @@ pkgs = import nixpkgs { inherit system overlays; }; - local-rust = (pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain).override { + local-rust = pkgs.rust-bin.stable.latest.default.override { extensions = [ "rust-analysis" ]; }; - local-nightly-rust = (pkgs.rust-bin.fromRustupToolchainFile ./plugins/grep/rust-toolchain.toml).override { - extensions = [ "rust-analysis" ]; - }; - nightly-cargo = pkgs.writeShellScriptBin "nightly-cargo" '' - export RUSTC="${local-nightly-rust}/bin/rustc"; - exec "${local-nightly-rust}/bin/cargo" "$@" - ''; - fixed-odin = pkgs.odin.overrideAttrs (finalAttrs: prevAttr: rec { - src = pkgs.fetchFromGitHub { - owner = "pcleavelin"; - repo = "Odin"; - rev = "7b9ea9eca02bf5dd295439a46ed6103a0c4a44ff"; - hash = "sha256-pxvU5veB1NEYPfer5roiLp/od2Pv4l1jJah0OHwb5yo="; + tree-sitter-odin = pkgs.stdenv.mkDerivation { + pname = "tree-sitter-odin"; + version = "1.3.0"; + + src = builtins.fetchGit { + url = "https://github.com/tree-sitter-grammars/tree-sitter-odin.git"; + rev = "e8adc739b78409a99f8c31313f0bb54cc538cf73"; }; - LLVM_CONFIG = "${pkgs.llvmPackages_17.llvm.dev}/bin/llvm-config"; - nativeBuildInputs = with pkgs; prevAttr.nativeBuildInputs ++ [ libcxx libcxxabi ]; - postPatch = prevAttr.postPatch + '' - sed -i build_odin.sh \ - -e 's|CXXFLAGS="$CXXFLAGS $($LLVM_CONFIG --cxxflags --ldflags)"|CXXFLAGS="$CXXFLAGS $($LLVM_CONFIG --cxxflags --ldflags) -I ${pkgs.libiconv.outPath}/include/"|' \ - -e 's|LDFLAGS="$LDFLAGS -pthread -lm -lstdc++"|LDFLAGS="$LDFLAGS -pthread -lm -lstdc++ -L ${pkgs.libiconv.outPath}/lib/ -L ${pkgs.llvmPackages_17.libcxxabi.outPath}/lib/"|' - ''; + installPhase = '' - runHook preInstall - - mkdir -p $out/bin - cp odin $out/bin/odin - - mkdir -p $out/share - cp -r core $out/share/core - cp -r vendor $out/share/vendor - - wrapProgram $out/bin/odin \ - --set PATH ${pkgs.lib.makeBinPath (with pkgs; [ - coreutils - llvmPackages_17.bintools - llvmPackages_17.lld - llvmPackages_17.clang - ])} \ - --set-default ODIN_ROOT $out/share - - runHook postInstall + mkdir -p $out/lib + cp libtree-sitter-odin.a $out/lib ''; - }); + }; + tree-sitter-json = pkgs.stdenv.mkDerivation { + pname = "tree-sitter-json"; + version = "0.24.8"; + + src = builtins.fetchGit { + url = "https://github.com/tree-sitter/tree-sitter-json.git"; + rev = "ee35a6ebefcef0c5c416c0d1ccec7370cfca5a24"; + }; + + installPhase = '' + mkdir -p $out/lib + cp libtree-sitter-json.a $out/lib + ''; + }; + tree-sitter-rust = pkgs.stdenv.mkDerivation { + pname = "tree-sitter-rust"; + version = "0.24.0"; + + src = builtins.fetchGit { + url = "https://github.com/tree-sitter/tree-sitter-rust.git"; + rev = "18b0515fca567f5a10aee9978c6d2640e878671a"; + }; + + installPhase = '' + mkdir -p $out/lib + cp libtree-sitter-rust.a $out/lib + ''; + }; + grep-lib = pkgs.rustPlatform.buildRustPackage rec { + name = "grep-lib"; + src = ./src/pkg/grep_lib; + nativeBuildInputs = [ local-rust ]; + + cargoLock = { + lockFile = ./src/pkg/grep_lib/Cargo.lock; + }; + + # lol, why does `buildRustPackage` not work without this? + # postPatch = '' + # ln -sf ${./src/pkg/grep_lib/Cargo.lock} Cargo.lock + # ''; + }; + tree-sitter = pkgs.stdenv.mkDerivation { + name = "tree-sitter"; + src = ./third_party/tree-sitter; + nativeBuildInputs = [ pkgs.clang ]; + + installPhase = '' + mkdir -p $out/lib + cp libtree-sitter.a $out/lib + ''; + }; in { devShell = pkgs.mkShell { buildInputs = with pkgs; (if pkgs.system == "aarch64-darwin" || pkgs.system == "x86_64-darwin" then [ - # fixed-odin - llvmPackages_17.bintools - llvmPackages_17.lld - llvmPackages_17.clang + git local-rust - nightly-cargo - rust-analyzer - lua54Packages.stdlib + odin + ols SDL2 SDL2_ttf - darwin.apple_sdk.frameworks.System - darwin.apple_sdk.frameworks.CoreData - darwin.apple_sdk.frameworks.Kernel - darwin.apple_sdk.frameworks.CoreVideo - darwin.apple_sdk.frameworks.GLUT - darwin.apple_sdk.frameworks.IOKit - darwin.apple_sdk.frameworks.OpenGL - darwin.apple_sdk.frameworks.Cocoa ] else if pkgs.system == "x86_64-linux" then [ pkg-config binutils odin + ols local-rust - rust-analyzer libGL xorg.libX11 xorg.libXi @@ -97,6 +108,33 @@ pkgs.nixgl.nixGLIntel ] else throw "unsupported system" ); }; + + packages = { + editor = pkgs.stdenv.mkDerivation rec { + pname = "editor"; + version = "0.1"; + src = ./.; + + buildInputs = with pkgs; [ + tree-sitter-odin + tree-sitter-json + tree-sitter-rust + tree-sitter + grep-lib + local-rust + binutils + clang + odin + ols + SDL2 + SDL2_ttf + ]; + installPhase = '' + mkdir -p $out/bin + cp bin/editor $out/bin/editor + ''; + }; + }; } ); } diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..416d834 --- /dev/null +++ b/readme.md @@ -0,0 +1,10 @@ +## How to build +* Install the [nix package manager](https://nixos.org/download/). +* Download [JetBrains Mono](https://www.jetbrains.com/lp/mono/) and place it in a new `bin` directory in the root of the project. +* Run `nix build .#editor` or `nix run .#editor`. + +## What is this? +This is an in-development modal text editor inspired by vim but with an actual GUI interface instead of yet another terminal program nobody asked for. + +## Is it currently usuable to edit files? +Yes, in a technical sense. It can load files, allow you to make edits to said file, and save the file. However, the *ergonomics* of using the editor aren't quite where I'd like them to be. It's almost at a point where I could bear using it to further improve it, but still wouldn't use it for real work. diff --git a/src/main.odin b/src/main.odin index 3da828e..5eb77a3 100644 --- a/src/main.odin +++ b/src/main.odin @@ -278,7 +278,7 @@ main :: proc() { defer ttf.Quit(); sdl_window := sdl2.CreateWindow( - "odin_editor - [less plugins more speed]", + "odin_editor - [now with `nix build`]", sdl2.WINDOWPOS_UNDEFINED, sdl2.WINDOWPOS_UNDEFINED, 640, diff --git a/src/panels/file_buffer.odin b/src/panels/file_buffer.odin index ae214b6..865ee6b 100644 --- a/src/panels/file_buffer.odin +++ b/src/panels/file_buffer.odin @@ -43,7 +43,7 @@ make_file_buffer_panel :: proc(file_path: string, line: int = 0, col: int = 0) - drop = proc(panel: ^core.Panel, state: ^core.State) { }, create = proc(panel: ^core.Panel, state: ^core.State) { - state_allocator = context.allocator + // state_allocator = context.allocator context.allocator = panel.allocator panel_state := &panel.type.(core.FileBufferPanel) @@ -752,4 +752,4 @@ file_buffer_text_input_actions :: proc(input_map: ^core.InputActions) { }, "Paste"); } -} \ No newline at end of file +} diff --git a/src/panels/grep.odin b/src/panels/grep.odin index f294d9e..6050b64 100644 --- a/src/panels/grep.odin +++ b/src/panels/grep.odin @@ -256,7 +256,7 @@ make_grep_panel :: proc() -> core.Panel { } } -foreign import grep_lib "../pkg/grep_lib/target/debug/libgrep.a" +foreign import grep_lib "system:grep_panel" @(default_calling_convention = "c") foreign grep_lib { grep :: proc (pattern: cstring, directory: cstring) -> RS_GrepResults --- diff --git a/src/pkg/grep_lib/Cargo.toml b/src/pkg/grep_lib/Cargo.toml index 70f36a1..154fee2 100644 --- a/src/pkg/grep_lib/Cargo.toml +++ b/src/pkg/grep_lib/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "grep" +name = "grep_panel" version = "0.1.0" edition = "2024" diff --git a/src/tree_sitter/ts.odin b/src/tree_sitter/ts.odin index 3c91b4d..9eba423 100644 --- a/src/tree_sitter/ts.odin +++ b/src/tree_sitter/ts.odin @@ -9,7 +9,7 @@ import "core:mem" import "../theme" -foreign import ts "../../bin/libtree-sitter.a" +foreign import ts "system:tree-sitter" @(default_calling_convention = "c", link_prefix="ts_") foreign ts { parser_new :: proc() -> Parser --- @@ -92,17 +92,17 @@ set_allocator :: proc(allocator := context.allocator) { } } -foreign import ts_odin "../../bin/libtree-sitter-odin.a" +foreign import ts_odin "system:tree-sitter-odin" foreign ts_odin { tree_sitter_odin :: proc "c" () -> Language --- } -foreign import ts_rust "../../bin/libtree-sitter-rust.a" +foreign import ts_rust "system:tree-sitter-rust" foreign ts_rust { tree_sitter_rust :: proc "c" () -> Language --- } -foreign import ts_json "../../bin/libtree-sitter-json.a" +foreign import ts_json "system:tree-sitter-json" foreign ts_json { tree_sitter_json :: proc "c" () -> Language --- } @@ -442,4 +442,4 @@ print_node_type :: proc(state: ^State) -> Maybe(string) { end_point.row+1, end_point.column+1) return string(node_type_str) -} \ No newline at end of file +}