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

Add blog post about using a nix flake for development with Loco #907

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
108 changes: 108 additions & 0 deletions docs-site/content/blog/nix-flake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
+++
title = "Loco development on NixOS"
description = "Set up your development shell with this nix flake to hit the ground running with Loco"
date = 2024-10-25T10:00:00+01:00
updated = 2024-10-25T10:00:00+01:00
draft = false
template = "blog/page.html"

[taxonomies]
authors = ["charludo"]

+++

## Overview

1. Create new Loco project
2. Add the `flake.nix`
3. Enter the development environment
4. Start Loco

## Create new Loco project

1. Run `loco new` to create a new project
2. Navigate through the instructions.

Note that the flake currently supports neither `postgres` nor `redis`, so make your choices accordingly.
Of course, if you have `redis`/`postgres` already running on your system, both are supported as usual - this flake just does not set them up for you!

## Add the `flake.nix`

Add the following as the contents of a new file `flake.nix`:

```nix
{
inputs = {
utils.url = "github:numtide/flake-utils";
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
};

outputs = { fenix, nixpkgs, utils, ... }:
utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
system = system;
};
toolchain = fenix.packages.${system}.latest;

# sea-orm-cli is on nixpkgs, but in a version too old for use with loco.rs
sea-orm-cli = pkgs.rustPlatform.buildRustPackage rec {
pname = "sea-orm-cli";
version = "1.1.0";

buildInputs = with pkgs; [ openssl ];
nativeBuildInputs = with pkgs; [ pkg-config ];

src = pkgs.fetchCrate {
inherit pname version;
hash = "sha256-qwWXHWo3gist1pTN5GlvjwyzXDLoKYcEEspy2gxJheA=";
};
cargoHash = "sha256-zKzJp1dBJnIWXxmx1JTiiolOydDVhJGM68zBZ3/BqAI=";
};
in
{
devShells.default = pkgs.mkShell {
nativeBuildInputs = with pkgs; [
loco-cli

pnpm
nodejs

(toolchain.withComponents [
"cargo"
"clippy"
"rust-src"
"rustc"
"rustfmt"
"rust-analyzer"
])
] ++ [ sea-orm-cli ];
};
});
}
```

This flake will provide you with everything you need to develop a Loco app with any of the supported frontends.

## Enter the development environment

1. Run `nix develop` inside the project directory.
2. Wait for a couple of seconds (building everything is only required the first time you enter the development shell).
3. If you don't want to have to invoke `nix develop` each time you want to work on your project, you could use a tool like `direnv` with the following `.envrc` file:
```bash
${DIRENV_DISABLE:+exit}
export DIRENV_DISABLE="1"
if [ -f flake.nix ]; then
use flake .
fi
```

## Start Loco

1. Start Loco with `cargo loco start`
2. Open http://localhost:5150/

Congrats, you are up and running with Loco on NixOS! :tada: