diff --git a/.envrc b/.envrc index b2cb254..1b070b6 100644 --- a/.envrc +++ b/.envrc @@ -1,5 +1,6 @@ watch_file \ *.cabal \ - nix/modules/flake/*.nix + haskell.nix \ + dev/* use flake diff --git a/README.md b/README.md index bccc1eb..2f2f663 100644 --- a/README.md +++ b/README.md @@ -13,18 +13,11 @@ If you have an *existing* Haskell project, you should probably use https://githu ## Getting Started -Initialize this template using: - -```sh -nix run nixpkgs#omnix -- \ - init github:srid/haskell-template -o ./yourproject -``` - -*tldr: [Install Nix](https://nixos.asia/en/install), [setup direnv](https://nixos.asia/en/direnv), open in VSCode, install recommended extensions and run `just run`.* +Initialize this template using the green "Use this template" button above or just clone it, and then [rename it](https://srid.ca/haskell-template/start#rename-the-project). Full instructions: https://srid.ca/haskell-template/start -Recommended dev environment setup: https://nixos.asia/en/direnv +*tldr: [Install Nix](https://nixos.asia/en/install), [setup direnv](https://nixos.asia/en/direnv), open in VSCode, install recommended extensions and run `just run`.* ## Tips @@ -39,6 +32,6 @@ Recommended dev environment setup: https://nixos.asia/en/direnv ## Discussions -Questions? Ideas? Suggestions? Join our [NixOS Asia community](https://nixos.asia/en/#community) or post in [Github Discussions](https://github.com/srid/haskell-template/discussions). +Questions? Ideas? Suggestions? Post in [Github Discussions](https://github.com/srid/haskell-template/discussions). [pre-commit]: https://github.com/cachix/git-hooks.nix diff --git a/nix/modules/flake/pre-commit.nix b/dev/flake-module.nix similarity index 50% rename from nix/modules/flake/pre-commit.nix rename to dev/flake-module.nix index dcb2d40..5a46a33 100644 --- a/nix/modules/flake/pre-commit.nix +++ b/dev/flake-module.nix @@ -1,12 +1,35 @@ -# Git pre-commit hooks are defined here. +# Development-only flake-parts module +# This module is loaded in the "dev" partition and includes devShell, pre-commit hooks, etc. { inputs, ... }: { imports = [ (inputs.git-hooks + /flake-module.nix) inputs.fourmolu-nix.flakeModule + ../haskell.nix ]; - perSystem = { config, ... }: { + + perSystem = { config, pkgs, ... }: { + # Development shell + devShells.default = pkgs.mkShell { + name = "haskell-template"; + meta.description = "Haskell development environment"; + + # See https://community.flake.parts/haskell-flake/devshell#composing-devshells + inputsFrom = [ + config.haskellProjects.default.outputs.devShell + config.pre-commit.devShell + ]; + + # Development packages + packages = with pkgs; [ + just + nixd + ghciwatch + ]; + }; + + # Git pre-commit hooks pre-commit.settings = { hooks = { nixpkgs-fmt.enable = true; diff --git a/dev/flake.lock b/dev/flake.lock new file mode 100644 index 0000000..74a75d2 --- /dev/null +++ b/dev/flake.lock @@ -0,0 +1,76 @@ +{ + "nodes": { + "fourmolu-nix": { + "locked": { + "lastModified": 1707266073, + "narHash": "sha256-tCFzZQJicDdYjnuJiNK4hiiRAH7c2wQzMhOCdUMbVKE=", + "owner": "jedimahdi", + "repo": "fourmolu-nix", + "rev": "717f5a91b0d7b97b1be7ecc3a0fd42d37ffe1c9b", + "type": "github" + }, + "original": { + "owner": "jedimahdi", + "repo": "fourmolu-nix", + "type": "github" + } + }, + "git-hooks": { + "flake": false, + "locked": { + "lastModified": 1760663237, + "narHash": "sha256-BflA6U4AM1bzuRMR8QqzPXqh8sWVCNDzOdsxXEguJIc=", + "owner": "cachix", + "repo": "git-hooks.nix", + "rev": "ca5b894d3e3e151ffc1db040b6ce4dcc75d31c37", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "git-hooks.nix", + "type": "github" + } + }, + "haskell-flake": { + "locked": { + "lastModified": 1760278819, + "narHash": "sha256-k6ctrfZ+5jO1vnqmQpkog0yXcXDGDbHxF6o5MWa2vKk=", + "owner": "srid", + "repo": "haskell-flake", + "rev": "3ab2a076aba01d932644f6f21e8aa507d28bb36b", + "type": "github" + }, + "original": { + "owner": "srid", + "repo": "haskell-flake", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1760596604, + "narHash": "sha256-J/i5K6AAz/y5dBePHQOuzC7MbhyTOKsd/GLezSbEFiM=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "3cbe716e2346710d6e1f7c559363d14e11c32a43", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "fourmolu-nix": "fourmolu-nix", + "git-hooks": "git-hooks", + "haskell-flake": "haskell-flake", + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/dev/flake.nix b/dev/flake.nix new file mode 100644 index 0000000..88963a2 --- /dev/null +++ b/dev/flake.nix @@ -0,0 +1,14 @@ +{ + description = "Development inputs for haskell-template"; + + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; + git-hooks.url = "github:cachix/git-hooks.nix"; + git-hooks.flake = false; + fourmolu-nix.url = "github:jedimahdi/fourmolu-nix"; + haskell-flake.url = "github:srid/haskell-flake"; + }; + + # This flake exists only for its inputs; see ./flake-module.nix for outputs. + outputs = { ... }: { }; +} diff --git a/dev/partition.nix b/dev/partition.nix new file mode 100644 index 0000000..8acdb13 --- /dev/null +++ b/dev/partition.nix @@ -0,0 +1,24 @@ +# Partition configuration for development outputs +# This routes devShells and checks to the dev partition +{ inputs, ... }: + +{ + imports = [ + inputs.flake-parts.flakeModules.partitions + ]; + # Partition development outputs to avoid fetching dev-only inputs + # when users only need packages/apps + partitionedAttrs = { + devShells = "dev"; + checks = "dev"; + }; + + partitions.dev = { + extraInputsFlake = ./.; + module = { + imports = [ + ./flake-module.nix + ]; + }; + }; +} diff --git a/flake.lock b/flake.lock index 637b717..726f060 100644 --- a/flake.lock +++ b/flake.lock @@ -2,9 +2,7 @@ "nodes": { "flake-parts": { "inputs": { - "nixpkgs-lib": [ - "nixpkgs" - ] + "nixpkgs-lib": "nixpkgs-lib" }, "locked": { "lastModified": 1759362264, @@ -20,37 +18,6 @@ "type": "github" } }, - "fourmolu-nix": { - "locked": { - "lastModified": 1707266073, - "narHash": "sha256-tCFzZQJicDdYjnuJiNK4hiiRAH7c2wQzMhOCdUMbVKE=", - "owner": "jedimahdi", - "repo": "fourmolu-nix", - "rev": "717f5a91b0d7b97b1be7ecc3a0fd42d37ffe1c9b", - "type": "github" - }, - "original": { - "owner": "jedimahdi", - "repo": "fourmolu-nix", - "type": "github" - } - }, - "git-hooks": { - "flake": false, - "locked": { - "lastModified": 1760663237, - "narHash": "sha256-BflA6U4AM1bzuRMR8QqzPXqh8sWVCNDzOdsxXEguJIc=", - "owner": "cachix", - "repo": "git-hooks.nix", - "rev": "ca5b894d3e3e151ffc1db040b6ce4dcc75d31c37", - "type": "github" - }, - "original": { - "owner": "cachix", - "repo": "git-hooks.nix", - "type": "github" - } - }, "haskell-flake": { "locked": { "lastModified": 1760278819, @@ -66,21 +33,6 @@ "type": "github" } }, - "nixos-unified": { - "locked": { - "lastModified": 1759968479, - "narHash": "sha256-YbcoDcei1PkY+d+h52Utnyw3Hgr6trnvDF3iqHWhkXM=", - "owner": "srid", - "repo": "nixos-unified", - "rev": "05fd33594509b21dd9d5a782fc0c5650acc55e61", - "type": "github" - }, - "original": { - "owner": "srid", - "repo": "nixos-unified", - "type": "github" - } - }, "nixpkgs": { "locked": { "lastModified": 1760596604, @@ -97,13 +49,25 @@ "type": "github" } }, + "nixpkgs-lib": { + "locked": { + "lastModified": 1754788789, + "narHash": "sha256-x2rJ+Ovzq0sCMpgfgGaaqgBSwY+LST+WbZ6TytnT9Rk=", + "owner": "nix-community", + "repo": "nixpkgs.lib", + "rev": "a73b9c743612e4244d865a2fdee11865283c04e6", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "nixpkgs.lib", + "type": "github" + } + }, "root": { "inputs": { "flake-parts": "flake-parts", - "fourmolu-nix": "fourmolu-nix", - "git-hooks": "git-hooks", "haskell-flake": "haskell-flake", - "nixos-unified": "nixos-unified", "nixpkgs": "nixpkgs" } } diff --git a/flake.nix b/flake.nix index 16f93ff..c1b3154 100644 --- a/flake.nix +++ b/flake.nix @@ -3,20 +3,15 @@ inputs = { nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; flake-parts.url = "github:hercules-ci/flake-parts"; - flake-parts.inputs.nixpkgs-lib.follows = "nixpkgs"; - nixos-unified.url = "github:srid/nixos-unified"; haskell-flake.url = "github:srid/haskell-flake"; - fourmolu-nix.url = "github:jedimahdi/fourmolu-nix"; - - git-hooks.url = "github:cachix/git-hooks.nix"; - git-hooks.flake = false; }; - outputs = inputs: - # This will import ./nix/modules/flake/*.nix - # cf. https://nixos-unified.org/autowiring.html#flake-parts - # - # To write your own Nix, add or edit files in ./nix/modules/flake/ - inputs.nixos-unified.lib.mkFlake - { inherit inputs; root = ./.; }; + outputs = inputs@{ flake-parts, ... }: + flake-parts.lib.mkFlake { inherit inputs; } { + systems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ]; + imports = [ + ./haskell.nix + ./dev/partition.nix # Development stuff decoupled; https://flake.parts/options/flake-parts-partitions.html + ]; + }; } diff --git a/nix/modules/flake/haskell.nix b/haskell.nix similarity index 76% rename from nix/modules/flake/haskell.nix rename to haskell.nix index aa0cd57..b10ec44 100644 --- a/nix/modules/flake/haskell.nix +++ b/haskell.nix @@ -1,26 +1,29 @@ # haskell-flake configuration goes in this module. -{ root, inputs, ... }: +{ inputs, ... }: { imports = [ inputs.haskell-flake.flakeModule ]; - perSystem = { self', lib, config, pkgs, ... }: { + perSystem = { self', lib, ... }: { # Our only Haskell project. You can have multiple projects, but this template # has only one. # See https://github.com/srid/haskell-flake/blob/master/example/flake.nix haskellProjects.default = { + # The base project is at the flake root # To avoid unnecessary rebuilds, we filter projectRoot: # https://community.flake.parts/haskell-flake/local#rebuild - projectRoot = builtins.toString (lib.fileset.toSource { - inherit root; - fileset = lib.fileset.unions [ - (root + /src) - (root + /haskell-template.cabal) - (root + /LICENSE) - (root + /README.md) - ]; - }); + projectRoot = + let root = ./.; + in builtins.toString (lib.fileset.toSource { + inherit root; + fileset = lib.fileset.unions [ + (root + /src) + (root + /haskell-template.cabal) + (root + /LICENSE) + (root + /README.md) + ]; + }); # The base package set (this value is the default) # basePackages = pkgs.haskellPackages; diff --git a/nix/modules/flake/devshell.nix b/nix/modules/flake/devshell.nix deleted file mode 100644 index 0d3f62e..0000000 --- a/nix/modules/flake/devshell.nix +++ /dev/null @@ -1,25 +0,0 @@ -# Configuration for the project's Nix devShell -# You mostly want the `packages` option below. - -{ - perSystem = { config, pkgs, ... }: { - # Default shell. - devShells.default = pkgs.mkShell { - name = "haskell-template"; - meta.description = "Haskell development environment"; - - # See https://community.flake.parts/haskell-flake/devshell#composing-devshells - inputsFrom = [ - config.haskellProjects.default.outputs.devShell # See ./nix/modules/haskell.nix - config.pre-commit.devShell # See ./nix/modules/formatter.nix - ]; - - # Packages to be added to Nix devShell go here. - packages = with pkgs; [ - just - nixd - ghciwatch - ]; - }; - }; -} diff --git a/nix/modules/flake/template.nix b/nix/modules/flake/template.nix deleted file mode 100644 index 2e7d1e3..0000000 --- a/nix/modules/flake/template.nix +++ /dev/null @@ -1,66 +0,0 @@ -{ inputs, ... }: - -{ - flake = rec { - templates.default = { - description = "A batteries-included Haskell project template for Nix"; - path = builtins.path { path = inputs.self; }; - welcomeText = '' - Please see https://srid.ca/haskell-template/start - ''; - }; - - # https://omnix.page/om/init.html#spec - om.templates.haskell-template = { - template = templates.default; - params = [ - { - name = "author"; - description = "Author name"; - placeholder = "Sridhar Ratnakumar"; - } - { - name = "package-name"; - description = "Name of the Haskell package"; - placeholder = "haskell-template"; - } - { - name = "vscode"; - description = "Include the VSCode settings folder (./.vscode)"; - paths = [ ".vscode" ]; - value = true; - } - { - name = "github-ci"; - description = "Include GitHub Actions workflow configuration"; - paths = [ ".github" ]; - value = true; - } - { - name = "nix-template"; - description = "Keep the flake template in the project"; - paths = [ "**/template.nix" ]; - value = false; - } - ]; - tests = { - default = { - params = { - package-name = "foo"; - author = "John"; - vscode = false; - }; - asserts = { - source = { - ".github/workflows/ci.yaml" = true; - ".vscode" = false; - }; - packages.default = { - "bin/foo" = true; - }; - }; - }; - }; - }; - }; -}