From 6b8853c02793ed17bc404ac5d3ac30d576468e72 Mon Sep 17 00:00:00 2001 From: doprz <52579214+doprz@users.noreply.github.com> Date: Thu, 14 Aug 2025 21:49:49 -0500 Subject: [PATCH] feat(nix): add comprehensive nix flake Co-authored-by: Eveeifyeve --- .gitignore | 6 +++ CONTRIBUTING.md | 25 ++++++++++ README.md | 55 +++++++++++++++++++-- default.nix | 12 +++++ flake.lock | 127 ++++++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 125 +++++++++++++++++++++++++++++++++++++++++++++++ shell.nix | 12 +++++ 7 files changed, 357 insertions(+), 5 deletions(-) create mode 100644 default.nix create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 shell.nix diff --git a/.gitignore b/.gitignore index 9b0ebaef6..ef4dfcac6 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,9 @@ __debug_bin* demo/output/* coverage.out + +# Nix +result +result-* +.direnv +.envrc diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 3c2bab21d..0c99f4af6 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -60,6 +60,31 @@ To run lazygit from within the integrated terminal just go `go run main.go` This allows you to contribute to Lazygit without needing to install anything on your local machine. The Codespace has all the necessary tools and extensions pre-installed. +## Using Nix for development + +If you use Nix, you can leverage the included flake to set up a complete development environment with all necessary dependencies: + +```sh +nix develop +``` + +This will drop you into a development shell that includes: +* Latest Go toolchain +* golangci-lint for code linting +* git and make + +You can also build and run lazygit using nix: + +```sh +# Build lazygit +nix build + +# Run lazygit directly +nix run +``` + +The nix flake supports multiple architectures (x86_64-linux, aarch64-linux, x86_64-darwin, aarch64-darwin) and provides a consistent development environment across different systems. + ## Code of conduct Please note by participating in this project, you agree to abide by the [code of conduct]. diff --git a/README.md b/README.md index b322e81d3..bae6e6375 100644 --- a/README.md +++ b/README.md @@ -388,19 +388,64 @@ sudo zypper ar https://download.opensuse.org/repositories/devel:/languages:/go/$ sudo zypper ref && sudo zypper in lazygit ``` -### NixOs +### NixOS -On NixOs lazygit is packaged with nix and distributed via nixpkgs. -You can try the lazygit without installing it with: +#### Using lazygit from nixpkgs + +On NixOS, lazygit is packaged with nix and distributed via nixpkgs. +You can try lazygit without installing it with: ```sh nix-shell -p lazygit # or with flakes enabled nix run nixpkgs#lazygit ``` +Or you can add lazygit to your `configuration.nix` using the `environment.systemPackages` option. +More details can be found via NixOS search [page](https://search.nixos.org/). -Or you can add lazygit to you `configuration.nix` using the `environment.systemPackages` option. -More details can be found via NixOs search [page](https://search.nixos.org/). +#### Using the official lazygit flake + +This repository includes a nix flake that provides the latest development version and additional development tools: + +**Run lazygit directly from the repository:** +```sh +nix run github:jesseduffield/lazygit +# or from a local clone +nix run . +``` + +**Build lazygit from source:** +```sh +nix build github:jesseduffield/lazygit +# or from a local clone +nix build . +``` + +**Development environment:** +For contributors, the flake provides a development shell with Go toolchain, development tools, and dependencies: +```sh +nix develop github:jesseduffield/lazygit +# or from a local clone +nix develop +``` + +The development shell includes: +- Go toolchain +- git and make +- Proper environment variables for development + +**Using in other flakes:** +The flake also provides an overlay for easy integration into other flake-based projects: +```nix +{ + inputs.lazygit.url = "github:jesseduffield/lazygit"; + + outputs = { self, nixpkgs, lazygit }: { + # Use the overlay + nixpkgs.overlays = [ lazygit.overlays.default ]; + }; +} +``` ### Flox diff --git a/default.nix b/default.nix new file mode 100644 index 000000000..c48b1fb8c --- /dev/null +++ b/default.nix @@ -0,0 +1,12 @@ +(import ( + let + lock = builtins.fromJSON (builtins.readFile ./flake.lock); + nodeName = lock.nodes.root.inputs.flake-compat; + in + fetchTarball { + url = + lock.nodes.${nodeName}.locked.url + or "https://github.com/edolstra/flake-compat/archive/${lock.nodes.${nodeName}.locked.rev}.tar.gz"; + sha256 = lock.nodes.${nodeName}.locked.narHash; + } +) { src = ./.; }).defaultNix diff --git a/flake.lock b/flake.lock new file mode 100644 index 000000000..6d026ba12 --- /dev/null +++ b/flake.lock @@ -0,0 +1,127 @@ +{ + "nodes": { + "flake-compat": { + "locked": { + "lastModified": 1733328505, + "narHash": "sha256-NeCCThCEP3eCl2l/+27kNNK7QrwZB1IJCrXfrbv5oqU=", + "rev": "ff81ac966bb2cae68946d5ed5fc4994f96d0ffec", + "revCount": 69, + "type": "tarball", + "url": "https://api.flakehub.com/f/pinned/edolstra/flake-compat/1.1.0/01948eb7-9cba-704f-bbf3-3fa956735b52/source.tar.gz" + }, + "original": { + "type": "tarball", + "url": "https://flakehub.com/f/edolstra/flake-compat/1.tar.gz" + } + }, + "flake-parts": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib" + }, + "locked": { + "lastModified": 1754487366, + "narHash": "sha256-pHYj8gUBapuUzKV/kN/tR3Zvqc7o6gdFB9XKXIp1SQ8=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "af66ad14b28a127c5c0f3bbb298218fc63528a18", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1756217674, + "narHash": "sha256-TH1SfSP523QI7kcPiNtMAEuwZR3Jdz0MCDXPs7TS8uo=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "4e7667a90c167f7a81d906e5a75cba4ad8bee620", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-25.05", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-lib": { + "locked": { + "lastModified": 1753579242, + "narHash": "sha256-zvaMGVn14/Zz8hnp4VWT9xVnhc8vuL3TStRqwk22biA=", + "owner": "nix-community", + "repo": "nixpkgs.lib", + "rev": "0f36c44e01a6129be94e3ade315a5883f0228a6e", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "nixpkgs.lib", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1754340878, + "narHash": "sha256-lgmUyVQL9tSnvvIvBp7x1euhkkCho7n3TMzgjdvgPoU=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "cab778239e705082fe97bb4990e0d24c50924c04", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-compat": "flake-compat", + "flake-parts": "flake-parts", + "nixpkgs": "nixpkgs", + "systems": "systems", + "treefmt-nix": "treefmt-nix" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "treefmt-nix": { + "inputs": { + "nixpkgs": "nixpkgs_2" + }, + "locked": { + "lastModified": 1755934250, + "narHash": "sha256-CsDojnMgYsfshQw3t4zjRUkmMmUdZGthl16bXVWgRYU=", + "owner": "numtide", + "repo": "treefmt-nix", + "rev": "74e1a52d5bd9430312f8d1b8b0354c92c17453e5", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "treefmt-nix", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 000000000..0535e1017 --- /dev/null +++ b/flake.nix @@ -0,0 +1,125 @@ +{ + description = "A simple terminal UI for git commands"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05"; + systems.url = "github:nix-systems/default"; + flake-parts.url = "github:hercules-ci/flake-parts"; + flake-compat.url = "https://flakehub.com/f/edolstra/flake-compat/1.tar.gz"; + treefmt-nix.url = "github:numtide/treefmt-nix"; + }; + + outputs = + inputs@{ flake-parts, systems, ... }: + flake-parts.lib.mkFlake { inherit inputs; } { + systems = import systems; + imports = [ + inputs.treefmt-nix.flakeModule + ]; + + perSystem = + { + pkgs, + system, + ... + }: + let + goMod = builtins.readFile ./go.mod; + versionMatch = builtins.match ".*go[[:space:]]([0-9]+\\.[0-9]+)(\\.[0-9]+)?.*" goMod; + + goVersion = + if versionMatch != null then + builtins.head versionMatch + else + throw "Could not extract Go version from go.mod"; + + goOverlay = final: prev: { + go = prev."go_${builtins.replaceStrings [ "." ] [ "_" ] goVersion}"; + }; + + lazygit = pkgs.buildGoModule rec { + pname = "lazygit"; + version = "dev"; + + gitCommit = inputs.self.rev or inputs.self.dirtyRev or "dev"; + + src = ./.; + vendorHash = null; + + # Disable integration tests that require specific environment + doCheck = false; + + nativeBuildInputs = with pkgs; [ + git + makeWrapper + ]; + buildInputs = [ pkgs.git ]; + + ldflags = [ + "-s" + "-w" + "-X main.commit=${gitCommit}" + "-X main.version=${version}" + "-X main.buildSource=nix" + ]; + + postInstall = '' + wrapProgram $out/bin/lazygit \ + --prefix PATH : ${pkgs.lib.makeBinPath [ pkgs.git ]} + ''; + + meta = { + description = "A simple terminal UI for git commands"; + homepage = "https://github.com/jesseduffield/lazygit"; + license = pkgs.lib.licenses.mit; + maintainers = [ "jesseduffield" ]; + platforms = pkgs.lib.platforms.unix; + mainProgram = "lazygit"; + }; + }; + in + { + _module.args.pkgs = import inputs.nixpkgs { + inherit system; + overlays = [ goOverlay ]; + config = { }; + }; + + packages = { + default = lazygit; + inherit lazygit; + }; + + devShells.default = pkgs.mkShell { + name = "lazygit-dev"; + + buildInputs = with pkgs; [ + # Go toolchain + go + gotools + + # Development tools + git + gnumake + ]; + + # Environment variables for development + CGO_ENABLED = "0"; + }; + + treefmt = { + programs.nixfmt.enable = pkgs.lib.meta.availableOn pkgs.stdenv.buildPlatform pkgs.nixfmt-rfc-style.compiler; + programs.nixfmt.package = pkgs.nixfmt-rfc-style; + programs.gofmt.enable = true; + }; + + checks.build = lazygit; + }; + + flake = { + overlays.default = final: prev: { + lazygit = inputs.self.packages.${final.system}.lazygit; + }; + }; + }; +} diff --git a/shell.nix b/shell.nix new file mode 100644 index 000000000..459e1526e --- /dev/null +++ b/shell.nix @@ -0,0 +1,12 @@ +(import ( + let + lock = builtins.fromJSON (builtins.readFile ./flake.lock); + nodeName = lock.nodes.root.inputs.flake-compat; + in + fetchTarball { + url = + lock.nodes.${nodeName}.locked.url + or "https://github.com/edolstra/flake-compat/archive/${lock.nodes.${nodeName}.locked.rev}.tar.gz"; + sha256 = lock.nodes.${nodeName}.locked.narHash; + } +) { src = ./.; }).shellNix