math-optim/flake.nix

259 lines
7.6 KiB
Nix
Raw Normal View History

2022-06-14 16:46:11 +02:00
{
description = "solving a few mathematical optimisation tasks using well-known algorithms (mathematical informatics course semestral project)";
2022-06-14 16:46:11 +02:00
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-compat = {
url = "github:edolstra/flake-compat";
flake = false;
};
nix-filter = {
url = "github:numtide/nix-filter";
};
};
outputs = {
self,
nixpkgs,
nix-filter,
...
}: let
projname = "math-optim";
2022-12-24 11:30:22 +01:00
system.configurationRevision =
self.rev
or throw "Refusing to build from a dirty Git tree!";
nix.registry.nixpkgs.flake = nixpkgs;
2022-06-14 16:46:11 +02:00
# to work with older version of flakes
lastModifiedDate =
self.lastModifiedDate or self.lastModified or "19700101";
2022-12-24 11:30:22 +01:00
# lastModifiedDate = "19700101";
2022-06-14 16:46:11 +02:00
# Generate a user-friendly version number.
version = "v0.0.0";
# System types to support.
supportedSystems = ["x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin"];
2022-06-14 16:46:11 +02:00
# Helper function to generate an attrset '{ x86_64-linux = f "x86_64-linux"; ... }'.
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
# Nixpkgs instantiated for supported system types.
nixpkgsFor = forAllSystems (system:
import nixpkgs {
inherit system;
overlays = [
# no overlay imports atm
2023-01-12 23:17:37 +01:00
# (import ./overlay.nix)
2022-06-14 16:46:11 +02:00
];
});
in rec {
formatter = forAllSystems (
system:
nixpkgsFor.${system}.alejandra
);
packages = forAllSystems (system: let
pkgs = nixpkgsFor.${system};
inherit (pkgs) lib overlays; # -> lib = pkgs.lib;overlays = pkgs.overlays;
in rec {
math-optim = with pkgs;
2023-02-08 20:10:38 +01:00
buildGo119Module rec {
2022-06-14 16:46:11 +02:00
pname = "${projname}";
buildInputs = [
2023-02-08 20:10:38 +01:00
go_1_19
2022-12-24 11:30:22 +01:00
# gcc
# glibc
# glibc.static
2022-06-14 16:46:11 +02:00
];
nativeBuildInputs = [pkgconfig];
2022-12-24 11:30:22 +01:00
# nativeBuildInputs = [go glibc.static];
2022-06-14 16:46:11 +02:00
overrideModAttrs = _: {
# GOPROXY = "direct";
2022-08-12 21:45:26 +02:00
GOFLAGS = "-buildmode=pie -trimpath -mod=readonly -modcacherw";
2022-06-14 16:46:11 +02:00
};
inherit version;
doCheck = false;
# use go.mod for managing go deps, instead of vendor-only dir
proxyVendor = true;
tags = []; # go "-tags" to build with
ldflags = [
"-s"
"-w"
"-X main.version=${version}"
];
# dont't forget to update vendorSha256 whenever go.mod or go.sum change
vendorSha256 = "sha256-JNvU72sb0m5bvKz/D6VAGsQiPh2lKt7N2DuhaOp6XA4=";
2022-06-14 16:46:11 +02:00
# In 'nix develop', we don't need a copy of the source tree
# in the Nix store.
src = nix-filter.lib.filter {
# when in doubt, check out
# https://github.com/numtide/nix-filter#design-notes
# tl;dr: it'd be best to include folders, however there are
# currently issues with that approach.
2022-08-22 16:27:44 +02:00
root = lib.cleanSource self;
2022-06-14 16:46:11 +02:00
exclude = [
./flake.nix
./flake.lock
./default.nix
./shell.nix
./overlay.nix
./README.md
2022-08-12 20:48:02 +02:00
./.envrc
./.drone.star
./.gitattributes
./.gitignore
./.golangci.yml
./.editorconfig
./.pre-commit-config.yaml
./.badges
# math-optim program output
./out
./res
# nix result symlink
./result
# the entire .git folder
./.git
2022-06-14 16:46:11 +02:00
];
};
meta = {
description = "solving a few mathematical optimisation tasks using well-known algorithms (originally a mathematical informatics course semestral project)";
2022-06-14 16:46:11 +02:00
homepage = "https://git.dotya.ml/wanderer/math-optim";
license = lib.licenses.gpl3;
maintainers = ["wanderer"];
platforms = lib.platforms.linux ++ lib.platforms.darwin;
};
};
default = math-optim;
});
apps = forAllSystems (system: rec {
math-optim = {
type = "app";
program = "${self.packages.${system}.${projname}}/bin/${projname}";
};
default = math-optim;
});
devShells = forAllSystems (
system: let
pkgs = import nixpkgs {
inherit system;
overlays = [
2023-02-08 20:10:38 +01:00
# (import ./overlay.nix)
2022-06-14 16:46:11 +02:00
];
};
goPkgs = import nixpkgs {
useFetched = true;
inherit system;
overlays =
self.overlays
or []
++ [
2023-02-08 20:10:38 +01:00
# (import ./overlay.nix)
2022-06-14 16:46:11 +02:00
];
};
gob = pkgs.writeShellScriptBin "gob" ''
cd $(git rev-parse --show-toplevel)
go build -v ./...
'';
gota = pkgs.writeShellScriptBin "gota" ''
cd $(git rev-parse --show-toplevel)
go test ./...
'';
2022-06-20 17:34:48 +02:00
gogen = pkgs.writeShellScriptBin "gogen" ''
cd $(git rev-parse --show-toplevel)
go generate -v ./report
'';
2022-06-14 16:46:11 +02:00
upcache = pkgs.writeShellScriptBin "upcache" ''
## refs:
## https://fzakaria.com/2020/08/11/caching-your-nix-shell.html
## https://nixos.wiki/wiki/Caching_nix_shell_build_inputs
nix-store --query --references $(nix-instantiate shell.nix) | \
xargs nix-store --realise | \
xargs nix-store --query --requisites | \
cachix push ${projname}
2022-08-12 21:42:00 +02:00
nix build --json \
| jq -r '.[].outputs | to_entries[].value' \
| cachix push ${projname}
2022-06-14 16:46:11 +02:00
'';
add-license = pkgs.writeShellScriptBin "add-license" ''
go run github.com/google/addlicense@v1.0.0 -v \
-c "wanderer <a_mirre at utb dot cz>" \
-l "GPL-3.0-or-later" -s .
'';
in {
default = with pkgs;
mkShell
{
name = "${projname}-" + version;
2022-12-24 11:30:22 +01:00
dontAutoPatchelf = "";
2022-06-14 16:46:11 +02:00
GOFLAGS = "-buildmode=pie -trimpath -mod=readonly -modcacherw";
GOLDFLAGS = "-s -w -X main.version=${version}";
2022-12-24 11:30:22 +01:00
CGO_CFLAGS = "-g0 -Ofast -mtune=native -flto";
2022-06-14 16:46:11 +02:00
CGO_LDFLAGS = "-Wl,-O1,-sort-common,-as-needed,-z,relro,-z,now,-flto -pthread";
2022-12-24 11:30:22 +01:00
# GOLDFLAGS = "-s -w -X main.version=${version} -linkmode external -extldflags -static";
# CGO_CFLAGS = "-g0 -mtune=native
# -I${pkgs.glibc.dev}/include
# ";
# LDFLAGS = "-L${pkgs.glibc}/lib";
# CGO_LDFLAGS = "
# -Wl,-O1,-sort-common,-as-needed,-z,relro,-z,now,-flto -pthread
# -L${pkgs.glibc}/lib
# ";
# GOPROXY = "direct";
2022-12-24 11:30:22 +01:00
# GOMEMLIMIT = "10GiB";
2022-06-14 16:46:11 +02:00
shellHook = ''
echo " -- in math-optim dev shell..."
'';
nativeBuildInputs = [
];
packages =
[
# use pathed go, as it's (supposed to be) faster with cgo,
# which will inevitably encountered anyway.
2023-01-12 23:17:37 +01:00
# goPkgs.go
2023-02-08 20:10:38 +01:00
go_1_19
2022-06-14 16:46:11 +02:00
go-tools
gopls
gofumpt
# golangci-lint
2022-06-14 16:46:11 +02:00
statix
alejandra
2023-01-13 22:10:26 +01:00
cachix
2022-06-14 16:46:11 +02:00
2022-12-24 11:30:22 +01:00
# glibc.static
2022-06-14 16:46:11 +02:00
## ad-hoc cmds
gob
gota
2022-06-20 17:34:48 +02:00
gogen
2022-06-14 16:46:11 +02:00
upcache
add-license
]
++ (
if stdenv.isLinux
then [
]
else []
);
};
}
);
};
}