Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve flake.nix to save build time, make nix run work again, and check builds on CI #312

Merged
merged 18 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 4 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

210 changes: 101 additions & 109 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -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];
};
});
};
}