From ad13581932bcec1452b039da0c345a6283ae77ed Mon Sep 17 00:00:00 2001 From: Akira Komamura <6270544+akirak@users.noreply.github.com> Date: Tue, 31 Oct 2023 22:34:55 +0900 Subject: [PATCH] chore(nix): improve flake and build in CI (#312) * style(nix): centralise the specification of Erlang and Elixir versions * chore(nix): switch to nixos-unstable channel The master branch is where PRs are merged into. Binary cache is more likely available on stable and unstable branches, so switch to unstable to use latest packages. * chore(nix): downgrade to Erlang R25 to prevent rebuild of rebar3 In NixOS/nixpkgs, the latest version of BEAM packages may not have all packages built on Hydra. In fact, rebar3 is not available from binary cache, which requires the package user to rebuild the package. Downgrading to the previous version of Erlang VM solves this issue. * fix(nix): fix hash mismatch of mixFodDeps * fix(nix)!: remove the broken flake app The path points to a location which apparently no longer exists. * chore(nix): add meta data of the Nix package Setting meta.mainProgram is recommended now so `nix run` consistently works. * style(nix): Use a map to manage the platform mappings * style(nix): tidy preInstall and postInstall hooks * style(nix): tidy * feat(nix): make the Erlang and Elixir versions overridable The user may want to use the same versions as his/her project under development, so it makes sense to allow overriding of beamPackages and elixir. * fix(nix): fix installation on ARM64 Linux * fix(nix): Revert to Erlang R26 as it is a requirement * style(nix): apply alejandra formatter * ci: add nix builds * fix(nix): Use the same version of Elixir for fetchMixDeps * update hash * fix ci --------- Co-authored-by: Mitchell Hanberg --- .github/workflows/ci.yaml | 11 ++ flake.lock | 7 +- flake.nix | 210 ++++++++++++++++++-------------------- 3 files changed, 116 insertions(+), 112 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 06e57647..188c06b3 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -118,3 +118,14 @@ jobs: - name: Run dialyzer run: mix dialyzer --format github + + nix-build: + strategy: + matrix: + os: [ubuntu-latest, macos-latest] + runs-on: ${{matrix.os}} + + steps: + - uses: actions/checkout@v2 + - uses: nixbuild/nix-quick-install-action@v26 + - run: nix build diff --git a/flake.lock b/flake.lock index 3d7a621b..2e18cc1b 100644 --- a/flake.lock +++ b/flake.lock @@ -2,15 +2,16 @@ "nodes": { "nixpkgs": { "locked": { - "lastModified": 1695669266, - "narHash": "sha256-QrCnbAcP9DtchKYaEC5LMpOBlpp2gBxua2626g3W41Q=", + "lastModified": 1698318101, + "narHash": "sha256-gUihHt3yPD7bVqg+k/UVHgngyaJ3DMEBchbymBMvK1E=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "7e5e43ef91a3828f92f8dd149d9af6521d0c736b", + "rev": "63678e9f3d3afecfeafa0acead6239cdb447574c", "type": "github" }, "original": { "owner": "NixOS", + "ref": "nixos-unstable", "repo": "nixpkgs", "type": "github" } diff --git a/flake.nix b/flake.nix index 4689e192..6f4c6008 100644 --- a/flake.nix +++ b/flake.nix @@ -1,124 +1,116 @@ { - inputs = { nixpkgs.url = "github:NixOS/nixpkgs"; }; + inputs = {nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";}; - outputs = { self, nixpkgs }: - let - lib = nixpkgs.lib; + outputs = { + self, + nixpkgs, + }: let + inherit (nixpkgs) lib; - # Systems supported - allSystems = [ - "x86_64-linux" # 64-bit Intel/AMD Linux - "aarch64-linux" # 64-bit ARM Linux - "x86_64-darwin" # 64-bit Intel macOS - "aarch64-darwin" # 64-bit ARM macOS - ]; - - pname = "next-ls"; - version = "0.14.2"; # x-release-please-version - src = ./.; - - # Helper to provide system-specific attributes - forAllSystems = f: - nixpkgs.lib.genAttrs allSystems (system: - let pkgs = import nixpkgs { inherit system; }; - in f { - inherit pkgs; - # src = pkgs.fetchFromGitHub { - # owner = "elixir-tools"; - # repo = "next-ls"; - # rev = "v${version}"; - # sha256 = "sha256-jpOInsr7Le0fjJZToNNrlNyXNF1MtF1kQONXdC2VsV0="; - # }; - system = system; - }); - - burritoExe = system: - if system == "aarch64-darwin" then - "darwin_arm64" - else if system == "x86_64-darwin" then - "darwin_amd64" - else if system == "x86_64-linux" then - "linux_amd64" - else if system == "aarch64-linux" then - "linux_arm64" - else - ""; - in { - packages = forAllSystems ({ pkgs, system }: - let + # Helper to provide system-specific attributes + forAllSystems = f: + nixpkgs.lib.genAttrs (builtins.attrNames burritoExe) (system: + f rec { + inherit system; + pkgs = nixpkgs.legacyPackages.${system}; beamPackages = pkgs.beam.packages.erlang_26; - build = type: - beamPackages.mixRelease { - inherit pname version src; - erlang = beamPackages.erlang; - elixir = beamPackages.elixir_1_15; + elixir = beamPackages.elixir_1_15; + }); - nativeBuildInputs = [ pkgs.xz pkgs.zig_0_11 pkgs._7zz ]; + burritoExe = { + "aarch64-darwin" = "darwin_arm64"; + "x86_64-darwin" = "darwin_amd64"; + "x86_64-linux" = "linux_amd64"; + "aarch64-linux" = "linux_arm64"; + }; + in { + packages = forAllSystems ({ + pkgs, + system, + beamPackages, + elixir, + }: rec { + default = lib.makeOverridable ({ + localBuild, + beamPackages, + elixir, + }: + beamPackages.mixRelease rec { + pname = "next-ls"; + version = "0.14.2"; # x-release-please-version + src = self.outPath; + inherit (beamPackages) erlang; + inherit elixir; - mixFodDeps = beamPackages.fetchMixDeps { - inherit src version; - pname = "${pname}-deps"; - hash = "sha256-ekB71eDfcFqC3JojFMnlGRQ/XiPwbUqFVor7ndpUd90="; - }; + nativeBuildInputs = [pkgs.xz pkgs.zig_0_11 pkgs._7zz]; - preConfigure = '' - bindir="$(pwd)/bin" - mkdir -p "$bindir" - echo '#!/usr/bin/env bash - 7zz "$@"' > "$bindir/7z" - chmod +x "$bindir/7z" + mixFodDeps = beamPackages.fetchMixDeps { + inherit src version elixir; + pname = "${pname}-deps"; + hash = "sha256-LV1DYmWi0Mcz1S5k77/jexXYqay7OpysCwOtUcafqGE="; + }; - export HOME="$(pwd)" - export PATH="$bindir:$PATH" - ''; + preConfigure = '' + bindir="$(pwd)/bin" + mkdir -p "$bindir" + echo '#!/usr/bin/env bash + 7zz "$@"' > "$bindir/7z" + chmod +x "$bindir/7z" - preBuild = '' - export BURRITO_ERTS_PATH=${beamPackages.erlang}/lib/erlang - ''; + export HOME="$(pwd)" + export PATH="$bindir:$PATH" + ''; - preInstall = if type == "local" then '' - export BURRITO_TARGET="${burritoExe (system)}" - '' else - ""; + preBuild = '' + export BURRITO_ERTS_PATH=${beamPackages.erlang}/lib/erlang + ''; - postInstall = let - beforeCommand = '' - chmod +x ./burrito_out/* - cp -r ./burrito_out "$out" - ''; - patchCommand = '' - patchelf --set-interpreter ${pkgs.glibc}/lib/ld-linux-x86-64.so.2 "$out/burrito_out/next_ls_linux_amd64" - ''; - afterCommand = '' - rm -rf "$out/bin" - mv "$out/burrito_out" "$out/bin" - mv "$out/bin/next_ls_${burritoExe (system)}" "$out/bin/nextls" - ''; - in beforeCommand - + lib.optionalString (system == "x86_64-linux") patchCommand - + afterCommand; - }; - in { - default = build ("local"); - ci = build ("ci"); - }); + preInstall = lib.optionalString localBuild '' + export BURRITO_TARGET="${burritoExe.${system}}" + ''; - apps = forAllSystems ({ pkgs, system, ... }: { - default = { - type = "app"; - program = "${self.packages.${system}.default}/burrito_out/next_ls_${ - burritoExe (system) - }"; - }; - }); + postInstall = '' + chmod +x ./burrito_out/* + cp -r ./burrito_out "$out" + ${lib.optionalString pkgs.stdenv.isLinux '' + patchelf --set-interpreter ${pkgs.stdenv.cc.libc}/lib/${ + if system == "x86_64-linux" + then "ld-linux-x86-64.so.2" + else if system == "aarch64-linux" + then "ld-linux-aarch64.so.1" + else throw "unsupported Linux system" + } \ + "$out/burrito_out/next_ls_${burritoExe.${system}}" + ''} + rm -rf "$out/bin" + mv "$out/burrito_out" "$out/bin" + mv "$out/bin/next_ls_${burritoExe.${system}}" "$out/bin/nextls" + ''; - devShells = forAllSystems ({ pkgs, ... }: - let beamPackages = pkgs.beam.packages.erlang_26; - in { - default = pkgs.mkShell { - # The Nix packages provided in the environment - packages = [ beamPackages.erlang beamPackages.elixir_1_15 ]; + meta = with lib; { + license = licenses.mit; + homepage = "https://www.elixir-tools.dev/next-ls/"; + description = "The language server for Elixir that just works"; + mainProgram = "nextls"; }; - }); - }; + }) { + inherit beamPackages elixir; + localBuild = true; + }; + + ci = default.override {localBuild = false;}; + }); + + devShells = forAllSystems ({ + pkgs, + beamPackages, + elixir, + ... + }: { + default = pkgs.mkShell { + # The Nix packages provided in the environment + packages = [beamPackages.erlang elixir]; + }; + }); + }; }