Skip to content

Instantly share code, notes, and snippets.

@Sylvance
Last active May 27, 2024 04:45
Show Gist options
  • Save Sylvance/70fff88aa5e535df11dc6b5984c5cebe to your computer and use it in GitHub Desktop.
Save Sylvance/70fff88aa5e535df11dc6b5984c5cebe to your computer and use it in GitHub Desktop.
Nix Rails Setup
---
BUNDLE_PATH: "vendor/bundle"

Prerequisite

Install nix with

Ensure that you run can run your Rails app with bin/rails. An example bin/rails has been provided for version 7 Rails.

Place all these files within the root of your Rails app

Run development workspace

Open the development workspace with:

nix develop .#devspace -c $SHELL

Build and run your application using Nix:

Build the application:

nix build .#my-rails-app

Run the application:

./result/bin/my-rails-app

Build and run your application using Docker:

Build the Docker image:

docker build -t my-rails-app .

Run the Docker container:

docker run --rm my-rails-app

Explanation

default.nix: Defines the build process for your Rails application using Nix. It specifies the build inputs, build commands, and install commands.

flake.nix: Defines the Nix flake, including inputs (Nixpkgs and flake-utils) and outputs (packages for different systems).

Dockerfile: Sets up a NixOS environment in a Docker container, installs Nix Flakes, copies your project files, builds your application using the flake, and sets the command to run your application.

By following these steps, you can build and run your Rails application both directly using Nix and within a Docker container.

#!/usr/bin/env ruby
APP_PATH = File.expand_path("../config/application", __dir__)
require_relative "../config/boot"
require "rails/commands"
{ pkgs ? import <nixpkgs> { } }:
pkgs.stdenv.mkDerivation {
pname = "my-rails-app";
version = "1.0.0";
src = ./.;
buildInputs = [ pkgs.ruby_3_3 ];
buildPhase = ''
bundle install
'';
installPhase = ''
mkdir -p $out/bin
cp -r . $out/bin/
echo "./bin/rails server" >> $out/bin/my-rails-app
chmod +x $out/bin/my-rails-app
'';
}
{ pkgs }:
pkgs.devshell.mkShell {
name = "rails-devspace.dev";
packages = [
pkgs.pkg-config
pkgs.gcc
pkgs.sqlite
pkgs.gnumake
pkgs.yarn
pkgs.ruby_3_3
pkgs.libyaml.dev
pkgs.openssl_3_3.dev
pkgs.postgresql
pkgs.autoconf269
pkgs.automake
pkgs.autogen
pkgs.libtool
pkgs.libffi.dev
pkgs.gnum4
pkgs.secp256k1
pkgs.shared-mime-info
];
env = [
{
name = "PKG_CONFIG_PATH";
value =
"${pkgs.pkg-config}:${pkgs.openssl_1_1.dev}/lib/pkgconfig:${pkgs.libyaml.dev}/lib/pkgconfig:${pkgs.postgresql}/lib/pkgconfig:${pkgs.libffi.dev}/lib/pkgconfig:${pkgs.secp256k1}/lib/pkgconfig";
}
{
name = "LIBTOOL";
value = "${pkgs.libtool}";
}
{
name = "FREEDESKTOP_MIME_TYPES_PATH";
value =
"${pkgs.shared-mime-info}/share/mime/packages/freedesktop.org.xml";
}
{
name = "NIXPKGS_ALLOW_INSECURE";
value = "1";
}
];
commands = [{
name = "devspace";
category = "devshell";
help = "opens devspace shell";
command = ''
zellij -l layout.kdl
'';
}];
}
FROM nixos/nix
# Install Nix Flakes
RUN nix-env -iA nixpkgs.nixFlakes
# Copy the flake files
COPY . /app
WORKDIR /app
# Build the application
RUN nix --extra-experimental-features "nix-command flakes" build .#my-rails-app
# Run the application
CMD ["./result/bin/my-rails-app"]
{
description = "Devspace";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils";
devshell.url = "github:numtide/devshell";
};
outputs = { self, nixpkgs, flake-utils, devshell, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ devshell.overlays.default ];
config.permittedInsecurePackages = [ "openssl-1.1.1w" ];
};
in {
# system = "aarch64-darwin";
packages.${system} = {
default = pkgs.callPackage ./default.nix { };
my-rails-app = pkgs.callPackage ./default.nix { };
};
devShells = {
devspace = import ./devspace.nix { inherit pkgs; };
};
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment