Skip to content

Instantly share code, notes, and snippets.

View nh2's full-sized avatar

Niklas Hambüchen nh2

View GitHub Profile
@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.
@nh2
nh2 / nixos-rootfs.nix
Last active March 27, 2024 14:34
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 / gist:9e16ce512bd082075e4dcd86115b21bb
Created March 10, 2024 18:48
"fastboot getvar all" on submerged, repaired Google Pixel 4a that had stock Android on it
(bootloader) product:sunfish
(bootloader) serialno:17191JEC203691
(bootloader) variant:SM7 UFS
(bootloader) max-download-size:0x10000000
(bootloader) slot-suffixes:_a,_b
(bootloader) version-bootloader:s5-0.5-10252351
(bootloader) version-baseband:g7150-00112-230505-B-10075601
(bootloader) secure-boot:PRODUCTION
(bootloader) secure:yes
(bootloader) hw-revision:MP1.0
@nh2
nh2 / detect-nix-sandbox.md
Last active March 7, 2024 22:03
How to determine if the nix sandbox is in effect on a system

How to determine if the nix sandbox is in effect on a system

Do the same as the nix-info script, which nix-builds this file and inspects the exit code.

Short version:

nix-build --no-out-link -E 'import <nixpkgs/pkgs/tools/nix/info/multiuser.nix>' 2> /dev/null
@nh2
nh2 / rigid-transform-with-scale.py
Last active February 5, 2024 11:04
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.
@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
#!/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 / route53.py
Last active October 9, 2023 17:59 — forked from rmarchei/route53.py
route53 hook for dehydrated - python2 / python3 + boto2 version. Tested on Ubuntu 16.04
#!/usr/bin/env python3
# How to use:
#
# Ubuntu 16.04: apt install -y python-boto OR apt install -y python3-boto
#
# Specify the default profile on aws/boto profile files or use the optional AWS_PROFILE env var:
# AWS_PROFILE=example ./dehydrated -c -d example.com -t dns-01 -k /etc/dehydrated/hooks/route53.py
#
# Manually specify hosted zone:
@nh2
nh2 / makePython3JobScriptWithPythonPackages.nix
Created October 16, 2017 02:32
Nix code to conveniently write an inline Python script with Python dependencies
{
# Inspired by https://github.com/NixOS/nixpkgs/blob/3dea1972737f5ce7b2c5461fe20370bad10aae03/nixos/modules/system/boot/systemd.nix#L202
makePython3JobScriptWithPythonPackages = name: packagesSelectionFun: text:
let
shellEscape = (import <nixpkgs/nixos/modules/system/boot/systemd-lib.nix> (with pkgs; { inherit config pkgs lib; })).shellEscape;
mkScriptName = s: (builtins.replaceStrings [ "\\" ] [ "-" ] (shellEscape s) );
x = pkgs.writeTextFile { name = "unit-script.py"; executable = true; destination = "/bin/${mkScriptName name}"; text = "#!/usr/bin/env python3\n${text}"; };
deriv = pkgs.stdenv.mkDerivation {
name = mkScriptName name;
buildInputs = [ (pkgs.python36.withPackages (pythonPackages: packagesSelectionFun pythonPackages)) ];