dream2nix/flake.nix

338 lines
9.6 KiB
Nix
Raw Normal View History

{
2022-02-22 11:00:32 +03:00
description = "A framework for 2nix tools";
nixConfig = {
extra-trusted-public-keys = "nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs=";
extra-substituters = "https://nix-community.cachix.org";
};
inputs = {
nixpkgs.url = "nixpkgs/nixos-unstable";
2023-06-21 15:49:19 +03:00
nixpkgsV1.url = "nixpkgs/nixos-unstable";
flake-parts.url = "github:hercules-ci/flake-parts";
2023-02-18 22:06:01 +03:00
flake-parts.inputs.nixpkgs-lib.follows = "nixpkgs";
flake-compat.url = "github:edolstra/flake-compat";
flake-compat.flake = false;
2023-03-12 17:45:11 +03:00
drv-parts.url = "github:davhau/drv-parts";
drv-parts.inputs.nixpkgs.follows = "nixpkgs";
drv-parts.inputs.flake-parts.follows = "flake-parts";
drv-parts.inputs.flake-compat.follows = "flake-compat";
2023-03-12 17:45:11 +03:00
pre-commit-hooks.url = "github:cachix/pre-commit-hooks.nix";
pre-commit-hooks.inputs.nixpkgs.follows = "nixpkgs";
flake-utils-pre-commit.url = "github:numtide/flake-utils";
pre-commit-hooks.inputs.flake-utils.follows = "flake-utils-pre-commit";
devshell = {
url = "github:numtide/devshell";
flake = false;
};
### framework dependencies
# required for builder go/gomod2nix
gomod2nix = {
url = "github:tweag/gomod2nix";
flake = false;
};
2021-10-20 12:39:11 +03:00
# required for translator pip
mach-nix = {
url = "mach-nix";
flake = false;
};
2021-10-20 12:39:11 +03:00
# required for pinned python translator
nix-pypi-fetcher = {
url = "github:DavHau/nix-pypi-fetcher";
flake = false;
};
# required for utils.satisfiesSemver
poetry2nix = {
2022-11-23 20:18:37 +03:00
url = "github:nix-community/poetry2nix/1.36.0";
flake = false;
};
# required for builder rust/crane
crane = {
2023-04-27 21:20:05 +03:00
url = "github:ipetkov/crane/v0.12.1";
flake = false;
};
# required for haskell translators
all-cabal-json = {
url = "github:nix-community/all-cabal-json/hackage";
flake = false;
};
ghc-utils = {
url = "git+https://gitlab.haskell.org/bgamari/ghc-utils";
flake = false;
};
# required for Racket translators
pruned-racket-catalog = {
url = "github:nix-community/pruned-racket-catalog/catalog";
flake = false;
};
};
outputs = {
self,
devshell,
flake-parts,
gomod2nix,
mach-nix,
nixpkgs,
poetry2nix,
pre-commit-hooks,
crane,
all-cabal-json,
ghc-utils,
pruned-racket-catalog,
...
} @ inp: let
l = lib // builtins;
lib = nixpkgs.lib;
# To use dream2nix in non-flake + non-IFD enabled repos, the source code of dream2nix
# must be installed into these repos (using nix run dream2nix#install).
# The problem is, all of dream2nix' dependecies need to be installed as well.
# Therefore 'externalPaths' contains all relevant files of external projects
# which dream2nix depends on. Exactly these files will be installed.
externalPaths = {
mach-nix = [
"lib/extractor/default.nix"
"lib/extractor/distutils.patch"
"lib/extractor/setuptools.patch"
"LICENSE"
];
poetry2nix = [
"lib.nix"
"pep425.nix"
"semver.nix"
"LICENSE"
];
crane = [
"lib/buildDepsOnly.nix"
"lib/buildPackage.nix"
"lib/cargoBuild.nix"
"lib/cleanCargoToml.nix"
"lib/findCargoFiles.nix"
"lib/mkCargoDerivation.nix"
"lib/mkDummySrc.nix"
"lib/writeTOML.nix"
2022-12-08 23:55:30 +03:00
"lib/setupHooks/cargoHelperFunctionsHook.sh"
"lib/setupHooks/configureCargoCommonVarsHook.sh"
"lib/setupHooks/configureCargoVendoredDepsHook.sh"
"lib/setupHooks/installFromCargoBuildLogHook.sh"
"lib/setupHooks/inheritCargoArtifactsHook.sh"
"lib/setupHooks/installCargoArtifactsHook.sh"
"LICENSE"
];
};
# An interface to access files of external projects.
# This implementation accesses the flake inputs directly,
# but if dream2nix is used without flakes, it defaults
# to another implementation of that function which
# uses the installed external paths instead (see default.nix)
externalSources =
lib.genAttrs
(lib.attrNames externalPaths)
(inputName: inp."${inputName}");
/*
Inputs that are not required for building, and therefore not need to be
copied alongside a dream2nix installation.
*/
inputs = inp;
overridesDirs = [(toString ./overrides)];
# System independent dream2nix api.
# Produces flake-like output schema.
d2n-lib = import ./src/lib.nix {
inherit externalPaths externalSources inputs overridesDirs lib;
nixpkgsSrc = "${nixpkgs}";
2022-10-28 19:57:59 +03:00
};
perSystem = {
config,
pkgs,
system,
...
}: let
d2n = import ./src {
inherit externalPaths externalSources inputs lib pkgs;
dream2nixConfig = {inherit overridesDirs;};
};
docsCli = pkgs.callPackage ./src/utils/view-docs {
dream2nixDocsSrc = "${self}/docs/src";
};
in {
options = {
d2n = l.mkOption {
type = l.types.raw;
};
};
config = {
inherit d2n;
# all apps including cli, install, etc.
apps =
d2n.flakeApps
// {
# passes through extra flags to treefmt
format.type = "app";
2022-12-28 22:41:52 +03:00
format.program = let
path = lib.makeBinPath [
pkgs.alejandra
2022-12-28 22:41:52 +03:00
pkgs.python3.pkgs.black
];
in
l.toString
(pkgs.writeScript "format" ''
2022-12-28 22:41:52 +03:00
export PATH="${path}"
${pkgs.treefmt}/bin/treefmt --clear-cache "$@"
'');
docs.type = "app";
docs.program = "${docsCli}/bin/d2n-docs";
};
2022-03-27 18:11:32 +03:00
# a dev shell for working on dream2nix
# use via 'nix develop . -c $SHELL'
devShells = let
makeDevshell = import "${inp.devshell}/modules" pkgs;
mkShell = config:
(makeDevshell {
configuration = {
inherit config;
imports = [];
};
})
.shell;
in rec {
default = dream2nix-shell;
dream2nix-shell = mkShell {
devshell.name = "dream2nix-devshell";
2022-12-28 22:41:52 +03:00
packages = [
pkgs.alejandra
2023-07-08 19:24:09 +03:00
(pkgs.python3.withPackages (ps: [
pkgs.python3.pkgs.black
]))
2022-12-28 22:41:52 +03:00
];
commands =
[
{
package = pkgs.mdbook;
category = "documentation";
}
{
package = docsCli;
category = "documentation";
help = "CLI for listing and viewing dream2nix documentation";
}
{
package = pkgs.treefmt;
category = "formatting";
}
]
# using linux is highly recommended as cntr is amazing for debugging builds
++ lib.optional pkgs.stdenv.isLinux {
package = pkgs.cntr;
category = "debugging";
};
devshell.startup = {
preCommitHooks.text = self.checks.${system}.pre-commit-check.shellHook;
dream2nixEnv.text = ''
export NIX_PATH=nixpkgs=${nixpkgs}
export d2nExternalDir=${d2n.externalDir}
export dream2nixWithExternals=${d2n.dream2nixWithExternals}
if [ -e ./overrides ]; then
export d2nOverridesDir=$(realpath ./overrides)
else
export d2nOverridesDir=${./overrides}
echo -e "\nManually execute 'export d2nOverridesDir={path to your dream2nix overrides dir}'"
fi
if [ -e ../dream2nix ]; then
export dream2nixWithExternals=$(realpath ./src)
else
export dream2nixWithExternals=${./src}
echo -e "\nManually execute 'export dream2nixWithExternals={path to your dream2nix checkout}'"
fi
'';
};
};
};
checks = {
pre-commit-check = pre-commit-hooks.lib.${system}.run {
src = ./.;
hooks = {
treefmt = {
enable = true;
name = "treefmt";
pass_filenames = false;
entry = l.toString (pkgs.writeScript "treefmt" ''
#!${pkgs.bash}/bin/bash
export PATH="$PATH:${pkgs.alejandra}/bin"
${pkgs.treefmt}/bin/treefmt --clear-cache --fail-on-change
'');
};
};
};
};
packages = {
docs =
pkgs.runCommand
"dream2nix-docs"
{nativeBuildInputs = [pkgs.bash pkgs.mdbook];}
''
bash -c "
errors=$(mdbook build -d $out ${./.}/docs |& grep ERROR)
if [ \"$errors\" ]; then
exit 1
fi
"
'';
};
};
};
2022-04-08 13:59:24 +03:00
flake = {
lib = d2n-lib;
# kept for compat
lib2 = d2n-lib;
flakeModuleBeta = {
imports = [./src/modules/flake-parts];
dream2nix.lib = d2n-lib;
};
};
in
2023-02-18 22:06:01 +03:00
flake-parts.lib.mkFlake {inherit inputs;} {
imports = [
./templates
2023-03-12 17:45:11 +03:00
./v1/nix/modules/flake-parts/all-modules.nix
];
systems = [
"x86_64-linux"
"aarch64-linux"
"x86_64-darwin"
"aarch64-darwin"
];
inherit flake perSystem;
};
}