diff --git a/.envrc b/.envrc new file mode 100644 index 00000000..e3fecb32 --- /dev/null +++ b/.envrc @@ -0,0 +1,2 @@ +use flake + diff --git a/.gitignore b/.gitignore index 2dd76ec7..31f11ea3 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,6 @@ pcre_constants.include junk .vagrant/ + +.direnv/ +result diff --git a/flake.lock b/flake.lock new file mode 100644 index 00000000..113b30e7 --- /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": 1764242076, + "narHash": "sha256-sKoIWfnijJ0+9e4wRvIgm/HgE27bzwQxcEmo2J/gNpI=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "2fad6eac6077f03fe109c4d4eb171cf96791faa4", + "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 00000000..87c7e129 --- /dev/null +++ b/flake.nix @@ -0,0 +1,75 @@ +{ + description = "NGS - Next Generation Shell"; + + 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}; + + # Local source filter - exclude build artifacts + localSrc = builtins.path { + path = ./.; + name = "ngs-source"; + filter = + path: type: + let + baseName = baseNameOf path; + in + !(baseName == "build" || baseName == "result" || baseName == ".git" || baseName == ".direnv"); + }; + + # For local development, override the package to use local source + ngs = (pkgs.callPackage ./package.nix { }).overrideAttrs (oldAttrs: { + src = localSrc; + version = oldAttrs.version + "-dev"; + }); + in + { + packages = { + default = ngs; + ngs = ngs; + }; + + devShells.default = pkgs.mkShell { + inputsFrom = [ ngs ]; + + packages = + with pkgs; + [ + # Development tools + gdb + clang-tools # clangd, clang-format + ] + ++ lib.optionals stdenv.isLinux [ + valgrind + strace + ]; + + shellHook = '' + echo "╔══════════════════════════════════════════════════════════╗" + echo "║ NGS Development Environment ║" + echo "╠══════════════════════════════════════════════════════════╣" + echo "║ Build: cmake -B build && cmake --build build ║" + echo "║ Test: cd build && ctest --output-on-failure ║" + echo "║ Run: NGS_PATH=lib ./build/ngs ║" + echo "║ Package: nix build ║" + echo "╚══════════════════════════════════════════════════════════╝" + ''; + }; + + # For nix fmt + formatter = pkgs.nixpkgs-fmt; + } + ); +} diff --git a/package.nix b/package.nix new file mode 100644 index 00000000..3592723c --- /dev/null +++ b/package.nix @@ -0,0 +1,100 @@ +{ + lib, + stdenv, + fetchFromGitHub, + fetchurl, + cmake, + pkg-config, + pandoc, + gawk, + makeWrapper, + boehmgc, + json_c, + libffi, + pcre, + # Darwin-specific: GNU sed is required for build scripts + gnused, +}: + +stdenv.mkDerivation (finalAttrs: { + pname = "ngs"; + version = "0.2.17"; + + src = fetchFromGitHub { + owner = "ngs-lang"; + repo = "ngs"; + rev = "v${finalAttrs.version}"; + hash = "sha256-j7OAXHADc2LlabKxVgYiKeDDtLttDVIavhQZSGyPGlE="; + }; + + # NGS requires peg 0.1.18 specifically due to custom patches in build-scripts/ + # The build scripts patch the leg output for location tracking, and this + # patching is not compatible with newer peg versions (0.1.20+). + # CMake will download and build peg 0.1.18 automatically when leg is not found. + pegSrc = fetchurl { + url = "https://www.piumarta.com/software/peg/peg-0.1.18.tar.gz"; + hash = "sha256-IBk73Wc/x0h6OJN+KX//CKpzdRtjOghqwow7NIkPkIQ="; + }; + + nativeBuildInputs = + [ + cmake + pkg-config + pandoc + gawk + makeWrapper + ] + ++ lib.optionals stdenv.isDarwin [ + gnused + ]; + + buildInputs = [ + boehmgc + json_c + libffi + pcre + ]; + + # The build scripts require GNU sed; on Darwin we need to ensure it's found + # Also pre-populate the peg source to avoid network access during build + preConfigure = + '' + # Create the external project download directory + mkdir -p build/leg-prefix/src + + # Copy peg source to where CMake ExternalProject expects it + cp ${finalAttrs.pegSrc} build/leg-prefix/src/peg-0.1.18.tar.gz + '' + + lib.optionalString stdenv.isDarwin '' + export PATH="${gnused}/bin:$PATH" + ''; + + cmakeFlags = [ + "-DBUILD_MAN=ON" + ]; + + # Set NGS_PATH so the installed binary can find the standard library + postInstall = '' + wrapProgram $out/bin/ngs \ + --set NGS_PATH $out/lib/ngs + ''; + + meta = with lib; { + description = "Next Generation Shell - a powerful programming language and shell designed for Ops"; + longDescription = '' + NGS is a unique combination of select features borrowed from other + languages and original features. NGS was built from the ground up + focusing on daily systems engineering tasks. + + One way to think about NGS is bash plus data structures plus better + syntax and error handling. Scripting AWS is much easier with NGS, + there is a Declarative Primitives style library for that. + ''; + homepage = "https://ngs-lang.org/"; + changelog = "https://github.com/ngs-lang/ngs/blob/v${finalAttrs.version}/CHANGELOG.md"; + license = licenses.gpl3Only; + maintainers = with maintainers; [ ]; + mainProgram = "ngs"; + platforms = platforms.unix; + }; +})