Skip to content

Instantly share code, notes, and snippets.

View nh2's full-sized avatar

Niklas Hambüchen nh2

View GitHub Profile
@nh2
nh2 / migrations.sql
Last active May 8, 2025 17:16
Postgres migrations with advisory lock for DDL transactions
-- Creates the table recording the current migration.
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
BEGIN;
-- Note: `create or replace function` cannot be run concurrently; postgres
-- will error with "tuple concurrently updated" when that happens.
-- That's why we wrap it in a transaction-level exclusive advisory lock.
-- See http://stackoverflow.com/questions/40525684/tuple-concurrently-updated-when-creating-functions-in-postgresql-pl-pgsql/44101303
@nh2
nh2 / ceph-benaco.nix
Created January 19, 2024 18:33
Open-sourcing Benaco's NixOS Ceph service module
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.ceph-benaco;
inherit (pkgs.callPackage ../helpers.nix {}) ensureUnitExists;
in
@nh2
nh2 / tcp-nodelay.md
Last active December 28, 2024 02:27
Understanding TCP_NODELAY

I believe the following is the best way to work with Nagle's algorithm / TCP_NODELAY / TCP_CORK.

It is described in this RedHat manual and the verdict is:

  • Set TCP_NODELAY = 1, always.
  • If you can batch data for sending by creating a buffer manually, or using writev(), prefer that.
  • If you cannot (e.g. "when using different libraries that provides abstractions for layers" from the above manual):
    • Set TCP_CORK = 1, then write the data, then set TCP_CORK = 0.
  • This builds a packet in kernel space and then flushes it out.
@nh2
nh2 / nixos-rootfs.nix
Last active November 16, 2024 04:08
Example of how to build a NixOS root file system
# Build with:
# NIX_PATH=nixpkgs=$HOME/src/nixpkgs nix-build --no-link '<nixpkgs/nixos>' -A config.system.build.tarball -I nixos-config=thisfile.nix
# You can also use
# -A config.system.build.toplevel
# to build something you can browse locally (that uses symlinks into your nix store).
{config, pkgs, ...}:
{
# We need no bootloader, because the Chromebook can't use that anyway.
boot.loader.grub.enable = false;
@nh2
nh2 / matmult.hs
Created February 9, 2014 21:13
Example of how Haskell's forM_ [1..N] is slow and custom loops are fast (lack of fusion)
{-# LANGUAGE ScopedTypeVariables, BangPatterns #-}
-- Example of how forM_ [1..N] is slow
module Main (main) where
import Prelude hiding (read)
import Control.Monad
import Data.Vector ((!), Vector)
import qualified Data.Vector as V
#!/usr/bin/env bash
# Installs NixOS on an OVH server, wiping the server.
#
# This is for a specific server configuration; adjust where needed.
# Originally written for an OVH STOR-1 server.
#
# Prerequisites:
# * Create a LUKS key file at /root/benacofs-luks-key
# e.g. by copying it up.
@nh2
nh2 / rigid-transform-with-scale.py
Last active July 17, 2024 14:03
Rigidly (+scale) aligns two point clouds with know point-to-point correspondences, in Python with numpy
import numpy as np
import numpy.linalg
# Relevant links:
# - http://stackoverflow.com/a/32244818/263061 (solution with scale)
# - "Least-Squares Rigid Motion Using SVD" (no scale but easy proofs and explains how weights could be added)
# Rigidly (+scale) aligns two point clouds with know point-to-point correspondences
# with least-squares error.
# This `firefox-vm.nix` builds a simple NixOS VM with XFCE and Firefox inside.
#
# Build and run it with:
#
# nix-build '<nixpkgs/nixos>' -A vm -I nixos-config=./firefox-vm.nix
# result/bin/run-nixos-vm
#
# Log in as "root" with empty password.
#
# To delete the VM state (storage):
@nh2
nh2 / gadt-equality.hs
Last active April 4, 2024 14:59
Shows how to implement an `instance Eq` for a GADT wrapped in an existential type, in Haskell.
-- Shows how to implement an `instance Eq` for a GADT wrapped in an existential type.
{-# LANGUAGE GADTs #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE TypeOperators #-}
import Data.Type.Equality
-- | A GADT. The example would also work if it had more than 1 type parameter.