Snippets

These are code snippets for one-off task.

ghc

compare core output

# Bump with core output
cabal clean && cabal build --ghc-options="-ddump-simpl -ddump-to-file -dsuppress-uniques" lib:simple-dsp
# Save output
cp ./dist-newstyle/build/x86_64-linux/ghc-9.6.1/simple-dsp-0.1/build/src/SimpleDSP/IIR.dump-simpl .
# Make change, rebuild and compare diff
...

runtime profile

With `cabal.project.local`:

ignore-project: False
library-profiling: True
executable-profiling: True
tests: True
benchmarks: True
profiling: True

Run:

# Build/run the project with profiling options
cabal run -O2 --enable-library-profiling --enable-profiling --ghc-options="-fprof-late-inline" exe:my-exe -- +RTS -p -hy -l-au -RTS
# Render report
nix shell nixpkgs#haskellPackages.eventlog2html eventlog2html my-exe.eventlog

perf

create a flamegraph

# Record perf data
sudo perf record -a -g -- ma-command
# Generate flame graph
sudo perf script | ~/src/github.com/brendangregg/FlameGraph/stackcollapse-perf.pl --all | ~/src/github.com/brendangregg/FlameGraph/flamegraph.pl > perfjuicer.svg

git

delete merged branch

git branch --merged | egrep -v "(^\*|master|main|dev)" | xargs git branch -d

tar

Export reaper project stems

tar --exclude="*.mp3" --exclude="Backups" --exclude="peaks" -cvzf /srv/cdn.midirus.com/stem/2024-11-opCapacity.tar.gz opCapacity/

ffmpeg

Encode mp3 with pochette for youtube

ffmpeg -loop 1 -r 1 -i opFreak-album.jpg -i opFreak-album.mp3 -c:a copy -shortest -c:v libx264 opFreak-album.mp4

Encode mp3

ffmpeg -i infile.mp3 -sample_fmt s16 out.flac
metaflac --preserve-modtime --remove-all-tags --set-tag=artist=Midirus --set-tag=album=Prelude --set-tag=year=2013 --set-tag=title=Berlin infile.flac
ffmpeg -i infile.flac -map_metadata 0 -id3v2_version 3 outfile.mp3
touch -r infile.flac outfile.mp3

Encode video

  • -movflags +faststart for fast play in browser
  • -crf 0 is lossless (for 8 bit only, for 10 bit use -qp 0), 23 is the default, and 51 is worst quality possible. 17/18 is visually lossless
  • -maxrate ensure the bitstream is reasonable
ffmpeg -i input-file -movflags +faststart -c:v libx264 -crf 23 -maxrate 1M -bufsize 2M -c:a copy 2021-12-22-opFreak.mp4

Draw spectrogram

ffmpeg -i file.wav -lavfi showspectrumpic=s=1920x1080:mode=separate spectrogram.png

Encode for op1

ffmpeg -i stem.flac -c:a pcm_s16le -ac 1 track_4.aif

gnome

Show wifi passwords

nmcli -s connection show $SSID | grep -i psk

rpm

List installed by size

# rpm -qia|awk '$1=="Name" {n=$3} $1=="Size" {s=$3} $1=="Description" {print s " " n }' | sort -n
rpm -qa --queryformat '%{SIZE} %{NAME}\n'

wayland

Disable input

sudo libinput list-devices
# grab the input path and run:
sudo nix run nixpkgs#evtest --grab /dev/input/event3

x11

mouse speeds

xinputs -> get the mouse id
xinputs list-props -> get the matrix prop id
xinput set-prop $MID $PID 3.0, 0.0, 0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 1.0

gpg

Update expired keys

$ gpg --edit-key EB103DE8B5E69E631C6FF17922B9A05C925CC5D8
gpg> expire
...
gpg> key 1
gpg> expire
...
gpg> save
$ gpg  --send-keys  EB103DE8B5E69E631C6FF17922B9A05C925CC5D8

Add ssh key

From https://opensource.com/article/19/4/gpg-subkeys-ssh

$ gpg2 --expert --edit-key <KEY ID>
gpg> addkey
   (8) RSA (set your own capabilities)
Your selection? 8

Possible actions for a RSA key: Sign Encrypt Authenticate
Current allowed actions: Authenticate

   (Q) Finished

Your selection? q
Is this correct? (y/N) y
Really create? (y/N) y
gpg> quit
Save changes? (y/N) y

In gpg.conf

enable-ssh-support

Auto add key to agent

gpg2 -K --with-keygrip
echo ID >> ~/.gnupg/sshcontrol

Then in .xinitrc

gpg-agent --daemon --keep-display
export SSH_AUTH_SOCK=$(gpgconf --list-dirs agent-ssh-socket)

ssh

Get public key from private key

ssh-keygen -y -f id_rsa

image magic

Compare test

// Use imagemagick to test that your image is what you want
'compare -metric RMSE actual.png expected.png',

pdf

split in half a double-page scanned PDF in a single pass:

mutool poster -r -x 2 in.pdf out.pdf

gnuplot

nice looking charts from CSV

From https://raymii.org/s/tutorials/GNUplot_tips_for_nice_looking_charts_from_a_CSV_file.html

set datafile separator ','
set xdata time
set timefmt "%Y-%m-%dT%H:%M:%S"
set key autotitle columnhead
set ylabel "First Y Units"
set xlabel 'Time'
set y2tics
set ytics nomirror
set y2label "Second Y Axis Value"
set style line 100 lt 1 lc rgb "grey" lw 0.5
set grid ls 100
set ytics 0.5
set xtics 1
set style line 101 lw 3 lt rgb "#f62aa0"
set style line 102 lw 3 lt rgb "#26dfd0"
set style line 103 lw 4 lt rgb "#b8ee30"

set xtics rotate # rotate labels on the x axis
set key right center # legend placement

plot filename using 1:2 with lines ls 101, '' using 1:3 with lines ls 102, '' using 1:4 with lines axis x1y2 ls 103

google calendar

Export to org mode:

Download archive from https://calendar.google.com/calendar/r/settings/export, then

TZ=Asia/Seoul ical2orgpy cal.ics gcal.org

notmuch

Delete mails matching query

notmuch search --format=text0 --output=files --exclude=false tag:monit | xargs -0 --no-run-if-empty rm

ZooKeeper

Connect with kazoo

import json
import kazoo.client
client = kazoo.client.KazooClient(hosts="zookeeper")
client.start()
def load_node(path, node, x):
    d = {}
    try:
      d = json.loads(node[0].decode('utf-8'))
      d['node_path'] = path + x
    except:
      print("error: ", x, node)
    return d


nodes = list(map(lambda x: load_node(client.get("/nodepool/nodes/" + x), x), client.get_children("/nodepool/nodes")))

# delete provider node
for node in nodes:
    if node.get('cloud') == 'rdo-cloud':
        print("deleting ", node.get('node_path'))
        client.delete(node['node_path'], recursive=True)

node = json.loads(client.get("/nodepool/nodes/" + nodes[0])[0].decode('utf-8'))

OpenStack

Deploy local instance

yum install openstack-nova openstack-neutron libvirt openstack-glance openstack-cinder openstack-heat-api openstack-heat-common openstack-heat-engine openstack-keystone openstack-nova-scheduler qemu-img-ev rabbitmq-server libvirt-daemon-kvm galera mariadb-server-galera memcached container-selinux openstack-selinux sos openstack-neutron-ml2 targetcli openvswitch
# resize hdd
fdisk /dev/vda
resize2fs /dev/vda

packstack --answer-file=basic

# Create cirros
curl -OL https://trunk.rdoproject.org/cirros-0.3.4-x86_64-disk.img
openstack image create --file cirros-0.3.4-x86_64-disk.img --disk-format qcow2 cirros2

# Remove router1
neutron router-gateway-clear router1
neutron router-port-list router1
neutron router-interface-delete router1 03916f57-a7a5-417d-b7d9-528b2fb4f993
neutron router-delete router1

# Remove public_subnet
neutron subnet-delete public_subnet

# Set br-ex network
/etc/sysconfig/network-scripts/ifcfg-br-ex
DEVICE=br-ex
DEVICETYPE=ovs
TYPE=OVSBridge
BOOTPROTO=static
IPADDR=192.168.100.2
NETMASK=255.255.255.0
GATEWAY=192.168.100.1
DNS1=192.168.42.1
ONBOOT=yes

/etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=OVSPort
DEVICETYPE=ovs
OVS_BRIDGE=br-ex
ONBOOT=yes

systemctl restart network

# Create public_subnet
neutron subnet-create --name public_subnet --enable_dhcp=True --allocation-pool=start=192.168.100.200,end=192.168.100.250  --gateway=192.168.100.1 public 192.168.100.0/24
# Create router1
neutron router-create router1
neutron router-gateway-set router1 public
neutron router-interface-add router1 private_subnet

# Create keypair
openstack keypair create --public-key kp id_rsa

openstack image set --public centos-7

# Enable ingress
openstack security group rule create --ingress default
openstack security group rule create --protocol icmp --ingress default

Gephi

See: https://github.com/TristanCacqueray/cabal-audit/?tab=readme-ov-file#analyze-with-gephi

files

Without ext: ${fp%.*} Last modified TS: stat -c "%Y %n" $fp Touch TS: touch --date @1349820671 $fp Kepp dates: touch -r src dst